forked from CodeForPhilly/clean-and-green-philly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeaturelayer.py
More file actions
407 lines (346 loc) · 15.4 KB
/
featurelayer.py
File metadata and controls
407 lines (346 loc) · 15.4 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import logging as log
import os
import subprocess
import traceback
import geopandas as gpd
import pandas as pd
import requests
import sqlalchemy as sa
from esridump.dumper import EsriDumper
from google.cloud import storage
from shapely import Point, wkb
from src.classes.bucket_manager import GCSBucketManager
from src.config.config import (
FORCE_RELOAD,
USE_CRS,
log_level,
min_tiles_file_size_in_bytes,
write_production_tiles_file,
)
from src.config.psql import conn, local_engine
log.basicConfig(level=log_level)
def google_cloud_bucket(require_write_access: bool = False) -> storage.Bucket | None:
"""
Initialize a Google Cloud Storage bucket client using Application Default Credentials.
If a writable bucket is requested and the user does not have write access None is returned.
Args:
require_write_access (bool): Whether it is required that the bucket should be writable. Defaults to False.
"""
bucket_manager = GCSBucketManager()
if require_write_access and bucket_manager.read_only:
return None
return bucket_manager.bucket
class FeatureLayer:
"""
FeatureLayer is a class to represent a GIS dataset. It can be initialized with a URL to an Esri Feature Service, a SQL query to Carto, or a GeoDataFrame.
"""
def __init__(
self,
name,
esri_rest_urls=None,
carto_sql_queries=None,
gdf=None,
crs=USE_CRS,
force_reload=FORCE_RELOAD,
from_xy=False,
use_wkb_geom_field=None,
cols: list[str] = None,
):
self.name = name
self.esri_rest_urls = (
[esri_rest_urls] if isinstance(esri_rest_urls, str) else esri_rest_urls
)
self.carto_sql_queries = (
[carto_sql_queries]
if isinstance(carto_sql_queries, str)
else carto_sql_queries
)
self.gdf = gdf
self.crs = crs
self.cols = cols
self.psql_table = name.lower().replace(" ", "_")
self.input_crs = "EPSG:4326" if not from_xy else USE_CRS
self.use_wkb_geom_field = use_wkb_geom_field
inputs = [self.esri_rest_urls, self.carto_sql_queries, self.gdf]
non_none_inputs = [i for i in inputs if i is not None]
if len(non_none_inputs) > 0:
if self.esri_rest_urls is not None:
self.type = "esri"
elif self.carto_sql_queries is not None:
self.type = "carto"
elif self.gdf is not None:
self.type = "gdf"
if force_reload:
self.load_data()
else:
psql_exists = self.check_psql()
if not psql_exists:
self.load_data()
else:
print(f"Initialized FeatureLayer {self.name} with no data.")
def check_psql(self):
try:
if not sa.inspect(local_engine).has_table(self.psql_table):
print(f"Table {self.psql_table} does not exist")
return False
psql_table = gpd.read_postgis(
f"SELECT * FROM {self.psql_table}", conn, geom_col="geometry"
)
if len(psql_table) == 0:
return False
else:
print(f"Loading data for {self.name} from psql...")
self.gdf = psql_table
return True
except Exception as e:
print(f"Error loading data for {self.name}: {e}")
return False
def load_data(self):
print(f"Loading data for {self.name} from {self.type}...")
if self.type == "gdf":
pass
else:
try:
if self.type == "esri":
if self.esri_rest_urls is None:
raise ValueError("Must provide a URL to load data from Esri")
gdfs = []
for url in self.esri_rest_urls:
parcel_type = (
"Land"
if "Vacant_Indicators_Land" in url
else "Building"
if "Vacant_Indicators_Bldg" in url
else None
)
self.dumper = EsriDumper(url)
features = [feature for feature in self.dumper]
geojson_features = {
"type": "FeatureCollection",
"features": features,
}
this_gdf = gpd.GeoDataFrame.from_features(
geojson_features, crs=self.input_crs
)
# Check if 'X' and 'Y' columns exist and create geometry if necessary
if "X" in this_gdf.columns and "Y" in this_gdf.columns:
this_gdf["geometry"] = this_gdf.apply(
lambda row: Point(row["X"], row["Y"]), axis=1
)
elif "geometry" not in this_gdf.columns:
raise ValueError(
"No geometry information found in the data."
)
this_gdf = this_gdf.to_crs(USE_CRS)
# Assign the parcel_type to the GeoDataFrame
if parcel_type:
this_gdf["parcel_type"] = parcel_type
gdfs.append(this_gdf)
self.gdf = pd.concat(gdfs, ignore_index=True)
elif self.type == "carto":
if self.carto_sql_queries is None:
raise ValueError(
"Must provide a SQL query to load data from Carto"
)
gdfs = []
for sql_query in self.carto_sql_queries:
response = requests.get(
"https://phl.carto.com/api/v2/sql", params={"q": sql_query}
)
data = response.json()["rows"]
df = pd.DataFrame(data)
geometry = (
wkb.loads(df[self.use_wkb_geom_field], hex=True)
if self.use_wkb_geom_field
else gpd.points_from_xy(df.x, df.y)
)
gdf = gpd.GeoDataFrame(
df,
geometry=geometry,
crs=self.input_crs,
)
gdf = gdf.to_crs(USE_CRS)
gdfs.append(gdf)
self.gdf = pd.concat(gdfs, ignore_index=True)
# Drop columns
if self.cols:
self.cols.append("geometry")
self.gdf = self.gdf[self.cols]
# save self.gdf to psql
# rename columns to lowercase for table creation in postgres
if self.cols:
self.gdf = self.gdf.rename(
columns={x: x.lower() for x in self.cols}
)
self.gdf.to_postgis(
name=self.psql_table,
con=conn,
if_exists="replace",
chunksize=1000,
)
except Exception as e:
print(f"Error loading data for {self.name}: {e}")
traceback.print_exc()
self.gdf = None
def spatial_join(self, other_layer, how="left", predicate="intersects"):
"""
Spatial joins in this script are generally left intersect joins.
They also can create duplicates, so we drop duplicates after the join.
Note: We may want to revisit the duplicates.
"""
# If other_layer.gdf isn't a geodataframe, try to make it one
if not isinstance(other_layer.gdf, gpd.GeoDataFrame):
try:
other_layer.rebuild_gdf()
except Exception as e:
print(f"Error converting other_layer to GeoDataFrame: {e}")
return
self.gdf = gpd.sjoin(self.gdf, other_layer.gdf, how=how, predicate=predicate)
self.gdf.drop(columns=["index_right"], inplace=True)
self.gdf.drop_duplicates(inplace=True)
# Coerce opa_id to integer and drop rows where opa_id is null or non-numeric
self.gdf.loc[:, "opa_id"] = pd.to_numeric(self.gdf["opa_id"], errors="coerce")
self.gdf = self.gdf.dropna(subset=["opa_id"])
def opa_join(self, other_df, opa_column):
"""
Join 2 dataframes based on opa_id and keep the 'geometry' column from the left dataframe if it exists in both.
"""
# Coerce opa_column to integer and drop rows where opa_column is null or non-numeric
other_df.loc[:, opa_column] = pd.to_numeric(
other_df[opa_column], errors="coerce"
)
other_df = other_df.dropna(subset=[opa_column])
# Coerce opa_id to integer and drop rows where opa_id is null or non-numeric
self.gdf.loc[:, "opa_id"] = pd.to_numeric(self.gdf["opa_id"], errors="coerce")
self.gdf = self.gdf.dropna(subset=["opa_id"])
# Perform the merge
joined = self.gdf.merge(
other_df, how="left", right_on=opa_column, left_on="opa_id"
)
# Check if 'geometry' column exists in both dataframes and clean up
if "geometry_x" in joined.columns and "geometry_y" in joined.columns:
joined = joined.drop(columns=["geometry_y"]).copy() # Ensure a full copy
joined = joined.rename(columns={"geometry_x": "geometry"})
if opa_column != "opa_id":
joined = joined.drop(columns=[opa_column])
# Assign the joined DataFrame to self.gdf as a full copy
self.gdf = joined.copy()
self.rebuild_gdf()
def rebuild_gdf(self):
self.gdf = gpd.GeoDataFrame(self.gdf, geometry="geometry", crs=self.crs)
def create_centroid_gdf(self):
"""
Convert the geometry of the GeoDataFrame to centroids.
"""
self.centroid_gdf = self.gdf.copy()
self.centroid_gdf.loc[:, "geometry"] = self.gdf["geometry"].centroid
def build_and_publish(self, tiles_file_id_prefix: str) -> None:
"""
Builds PMTiles and a Parquet file from a GeoDataFrame and publishes them to Google Cloud Storage.
Args:
tiles_file_id_prefix (str): The ID prefix used for naming the PMTiles and Parquet files, coming from src.config.
Raises:
ValueError: Raised if the generated PMTiles file is smaller than the minimum allowed size, indicating a potential corruption or incomplete file.
"""
zoom_threshold: int = 13
# Export the GeoDataFrame to a temporary GeoJSON file
temp_geojson_points: str = f"tmp/temp_{tiles_file_id_prefix}_points.geojson"
temp_geojson_polygons: str = f"tmp/temp_{tiles_file_id_prefix}_polygons.geojson"
temp_pmtiles_points: str = f"tmp/temp_{tiles_file_id_prefix}_points.pmtiles"
temp_pmtiles_polygons: str = f"tmp/temp_{tiles_file_id_prefix}_polygons.pmtiles"
temp_merged_pmtiles: str = f"tmp/temp_{tiles_file_id_prefix}_merged.pmtiles"
temp_parquet: str = f"tmp/{tiles_file_id_prefix}.parquet"
# Reproject
gdf_wm = self.gdf.to_crs(epsg=4326)
gdf_wm.to_file(temp_geojson_polygons, driver="GeoJSON")
# Create points dataset
self.centroid_gdf = self.gdf.copy()
self.centroid_gdf["geometry"] = self.centroid_gdf["geometry"].centroid
self.centroid_gdf = self.centroid_gdf.to_crs(epsg=4326)
self.centroid_gdf.to_file(temp_geojson_points, driver="GeoJSON")
# Load the GeoJSON from the polygons, drop geometry, and save as Parquet
gdf_polygons = gpd.read_file(temp_geojson_polygons)
df_no_geom = gdf_polygons.drop(columns=["geometry"])
# Check if the DataFrame has fewer than 25,000 rows
num_rows, num_cols = df_no_geom.shape
if num_rows < 25000:
print(
f"Parquet file has {num_rows} rows, which is fewer than 25,000. Skipping upload."
)
return
# Save the DataFrame as Parquet
df_no_geom.to_parquet(temp_parquet)
# Upload Parquet to Google Cloud Storage
bucket = google_cloud_bucket(require_write_access=True)
if bucket is None:
print(
"Skipping Parquest and PMTiles upload due to read-only bucket access."
)
return
blob_parquet = bucket.blob(f"{tiles_file_id_prefix}.parquet")
try:
blob_parquet.upload_from_filename(temp_parquet)
parquet_size = os.stat(temp_parquet).st_size
parquet_size_mb = parquet_size / (1024 * 1024)
print(
f"Parquet upload successful! Size: {parquet_size} bytes ({parquet_size_mb:.2f} MB), Dimensions: {num_rows} rows, {num_cols} columns."
)
except Exception as e:
print(f"Parquet upload failed: {e}")
return
# Command for generating PMTiles for points up to zoom level zoom_threshold
points_command: list[str] = [
"tippecanoe",
f"--output={temp_pmtiles_points}",
f"--maximum-zoom={zoom_threshold}",
"--minimum-zoom=10",
"-zg",
"-aC",
"-r0",
temp_geojson_points,
"-l",
"vacant_properties_tiles_points",
"--force",
]
# Command for generating PMTiles for polygons from zoom level zoom_threshold
polygons_command: list[str] = [
"tippecanoe",
f"--output={temp_pmtiles_polygons}",
f"--minimum-zoom={zoom_threshold}",
"--maximum-zoom=16",
"-zg",
"--no-tile-size-limit",
temp_geojson_polygons,
"-l",
"vacant_properties_tiles_polygons",
"--force",
]
# Command for merging the two PMTiles files into a single output file
merge_command: list[str] = [
"tile-join",
f"--output={temp_merged_pmtiles}",
"--no-tile-size-limit",
temp_pmtiles_polygons,
temp_pmtiles_points,
"--force",
]
# Run the commands
for command in [points_command, polygons_command, merge_command]:
subprocess.run(command)
write_files: list[str] = [f"{tiles_file_id_prefix}_staging.pmtiles"]
if write_production_tiles_file:
write_files.append(f"{tiles_file_id_prefix}.pmtiles")
# Check whether the temp saved tiles files is big enough.
file_size: int = os.stat(temp_merged_pmtiles).st_size
if file_size < min_tiles_file_size_in_bytes:
raise ValueError(
f"{temp_merged_pmtiles} is {file_size} bytes in size but should be at least {min_tiles_file_size_in_bytes}. Therefore, we are not uploading any files to the GCP bucket. The file may be corrupt or incomplete."
)
# Upload PMTiles to Google Cloud Storage
for file in write_files:
blob = bucket.blob(file)
try:
blob.upload_from_filename(temp_merged_pmtiles)
print(f"PMTiles upload successful for {file}!")
except Exception as e:
print(f"PMTiles upload failed for {file}: {e}")