-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
58 lines (46 loc) · 1.63 KB
/
Copy pathutils.py
File metadata and controls
58 lines (46 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pandas as pd
import geopandas as gpd
import fiona
import shapely
from shapely.geometry import shape
from pathlib import Path
def load_gdf(filepath, crs: str):
filepath = Path(filepath)
if filepath.suffix == '.shp':
return load_shapefile(filepath, crs)
elif filepath.suffix == '.gpkg':
return load_geopackage(filepath, crs)
elif filepath.suffix == '.csv':
return load_csv(filepath, crs)
elif filepath.suffix == '.geojson':
return load_geojson(filepath)
def load_shapefile(filepath, crs):
print('loading shapefile:', filepath)
with fiona.open(filepath) as src:
geometries = [shape(feature['geometry']) for feature in src]
properties = [feature['properties'] for feature in src]
gdf = gpd.GeoDataFrame(properties, geometry=geometries)
return gdf.set_crs(crs)
def load_geopackage(filepath, crs):
print('loading geopackage:', filepath)
gdf = gpd.read_file(filepath)
return gdf.set_crs(epsg=crs)
def load_csv(filepath, crs):
print('loading csv:', filepath)
df = pd.read_csv(filepath)
try:
return gpd.GeoDataFrame(
df,
geometry=gpd.points_from_xy(df.lon, df.lat),
crs=f"EPSG:{crs}"
)
except Exception:
print("No 'lat' 'lon' fields found in csv, trying WKT")
try:
df["geometry"] = df["wkt_geom"].apply(shapely.wkt.loads)
return gpd.GeoDataFrame(df, geometry="geometry", crs=f"EPSG:{crs}")
except Exception:
print("No 'lat' 'lon' fields or 'wkt_geom' fields found in csv")
raise Exception
def load_geojson(filepath):
return gpd.read_file(filepath)