Skip to content

Commit

Permalink
Merge pull request #51 from heatherbaier/42-som-assign-geo-ids
Browse files Browse the repository at this point in the history
Created SOM geo ID's
  • Loading branch information
heatherbaier authored Sep 22, 2024
2 parents 8baed7c + 22028f1 commit 365399a
Show file tree
Hide file tree
Showing 8 changed files with 544 additions and 0 deletions.
454 changes: 454 additions & 0 deletions files_for_db/geo/som_geo.csv

Large diffs are not rendered by default.

Binary file added files_for_db/shps/som.zip
Binary file not shown.
1 change: 1 addition & 0 deletions files_for_db/shps/som/som.cpg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ISO-8859-1
Binary file added files_for_db/shps/som/som.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions files_for_db/shps/som/som.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]
Binary file added files_for_db/shps/som/som.shp
Binary file not shown.
Binary file added files_for_db/shps/som/som.shx
Binary file not shown.
88 changes: 88 additions & 0 deletions scripts/geo/som_geo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import geopandas as gpd
import pandas as pd
import shutil
import os
from utils import *

# Fetch and read the datasets
DATA_PATH = str(os.path.abspath(os.path.join(__file__ ,"../../.."))) + "/data/SOM/Somalia - Education Facilities/"
som_schools = gpd.read_file(DATA_PATH + "Somalia - Education Facilities.shp")

# Subset and rename required columns
som_schools = som_schools[["NAMEOFSCHO","LATITUDE","LONGITUDE",]]
# Rename column names
som_schools.rename(columns = {'LONGITUDE':'longitude','LATITUDE':'latitude','NAMEOFSCHO':'school_name'}, inplace = True)

# Drop unknown schools
som_schools = som_schools.dropna()

# Add dependency ID's to each dataset
som_deped_id = ["Somalia - Education Facilities" + f".{idx}" for idx in range(1,som_schools.shape[0]+1)]
som_schools["deped_id"] = som_deped_id

# Add geo ID's
som_schools = som_schools.reset_index()
som_schools['geo_id'] = som_schools["index"].apply(lambda x: 'SOM-{0:0>6}'.format(x))
som_schools = som_schools.drop(["index"], axis = 1)

# Add address column
som_schools["address"] = None

# Add ADM levels
som_schools["adm0"] = "SOM"
cols = ["geo_id", "deped_id", "school_name", "adm0", "address"]

longs = som_schools["longitude"].values
lats = som_schools["latitude"].values

# Add ADM1, ADM2, ADM3
for adm in range(1, 4):
try:
cols += ["adm" + str(adm)]
downloadGB("SOM", str(adm), "../../gb")
shp = gpd.read_file(getGBpath("SOM", f"ADM{str(adm)}", "../../gb"))
shp = shp.set_crs("EPSG:4326")
som_schools = gpd.GeoDataFrame(som_schools, geometry = gpd.points_from_xy(som_schools.longitude, som_schools.latitude))
# Set CRS?
som_schools = som_schools.set_crs("EPSG:4326")

if adm == 1:
som_schools = gpd.clip(som_schools, shp)
longs = som_schools["longitude"].values
lats = som_schools["latitude"].values

som_schools = gpd.tools.sjoin(som_schools, shp, how = "left").rename(columns = {"shapeName": "adm" + str(adm)})[cols]
som_schools["longitude"] = longs
som_schools["latitude"] = lats
print(som_schools.head())

except Exception as e:
som_schools["adm" + str(adm)] = None
print("Error!")
print(e)

som_schools = som_schools[["geo_id","deped_id","school_name","address","adm0","adm1","adm2","adm3","longitude","latitude"]]

# Add files
PATH = str(os.path.abspath(os.path.join(__file__ ,"../../..")))

# Add SOM csv file to files_for_db
som_schools.to_csv(PATH + "/files_for_db/geo/som_geo.csv", index = False)

gdf = gpd.GeoDataFrame(
som_schools,
geometry = gpd.points_from_xy(
x = som_schools.longitude,
y = som_schools.latitude,
crs = 'EPSG:4326', # or: crs = pyproj.CRS.from_user_input(4326)
)
)

if not os.path.exists(PATH + "/files_for_db/shps/som/"):
os.mkdir(PATH + "/files_for_db/shps/som/")

# Add shape file to files_for_db
gdf.to_file(PATH + "/files_for_db/shps/som/som.shp", index = False)

# Add zip file to files_for_db
shutil.make_archive(PATH + "/files_for_db/shps/som", 'zip', PATH + "/files_for_db/shps/som")

0 comments on commit 365399a

Please sign in to comment.