@@ -257,45 +257,45 @@ def extract_hwsd2_data(connection, lon, lat, buffer_dist, table_name):
257
257
# Distance is computed by casting geometries to geography,
258
258
# which returns the geodesic distance in meters.
259
259
# Q1
260
- main_query = f"""
261
- WITH
262
- -- Step 1: Get the polygon that contains the point
263
- point_poly AS (
264
- SELECT geom
265
- FROM { table_name }
266
- WHERE ST_Intersects(
267
- geom,
268
- ST_SetSRID(ST_Point({ lon } , { lat } ), 4326)
269
- )
270
- ),
260
+ # main_query = f"""
261
+ # WITH
262
+ # -- Step 1: Get the polygon that contains the point
263
+ # point_poly AS (
264
+ # SELECT geom
265
+ # FROM {table_name}
266
+ # WHERE ST_Intersects(
267
+ # geom,
268
+ # ST_SetSRID(ST_Point({lon}, {lat}), 4326)
269
+ # )
270
+ # ),
271
271
272
- -- Step 2: Get polygons that intersect the buffer
273
- valid_geom AS (
274
- SELECT
275
- hwsd2,
276
- geom
277
- FROM { table_name }
278
- WHERE geom && ST_GeomFromText('{ buffer_wkt } ', 4326)
279
- AND ST_Intersects(geom, ST_GeomFromText('{ buffer_wkt } ', 4326))
280
- )
272
+ # -- Step 2: Get polygons that intersect the buffer
273
+ # valid_geom AS (
274
+ # SELECT
275
+ # hwsd2,
276
+ # geom
277
+ # FROM {table_name}
278
+ # WHERE geom && ST_GeomFromText('{buffer_wkt}', 4326)
279
+ # AND ST_Intersects(geom, ST_GeomFromText('{buffer_wkt}', 4326))
280
+ # )
281
281
282
- -- Step 3: Filter to those that either contain the point or border the point's polygon
283
- SELECT
284
- vg.hwsd2,
285
- ST_AsEWKB(vg.geom) AS geom,
286
- ST_Distance(
287
- vg.geom::geography,
288
- ST_SetSRID(ST_Point({ lon } , { lat } ), 4326)::geography
289
- ) AS distance,
290
- ST_Intersects(
291
- vg.geom,
292
- ST_SetSRID(ST_Point({ lon } , { lat } ), 4326)
293
- ) AS pt_intersect
294
- FROM valid_geom vg, point_poly pp
295
- WHERE
296
- ST_Intersects(vg.geom, ST_SetSRID(ST_Point({ lon } , { lat } ), 4326))
297
- OR ST_Intersects(vg.geom, pp.geom);
298
- """
282
+ # -- Step 3: Filter to those that either contain the point or border the point's polygon
283
+ # SELECT
284
+ # vg.hwsd2,
285
+ # ST_AsEWKB(vg.geom) AS geom,
286
+ # ST_Distance(
287
+ # vg.geom::geography,
288
+ # ST_SetSRID(ST_Point({lon}, {lat}), 4326)::geography
289
+ # ) AS distance,
290
+ # ST_Intersects(
291
+ # vg.geom,
292
+ # ST_SetSRID(ST_Point({lon}, {lat}), 4326)
293
+ # ) AS pt_intersect
294
+ # FROM valid_geom vg, point_poly pp
295
+ # WHERE
296
+ # ST_Intersects(vg.geom, ST_SetSRID(ST_Point({lon}, {lat}), 4326))
297
+ # OR ST_Intersects(vg.geom, pp.geom);
298
+ # """
299
299
300
300
# # Q2
301
301
# main_query = f"""
@@ -326,20 +326,20 @@ def extract_hwsd2_data(connection, lon, lat, buffer_dist, table_name):
326
326
# FROM valid_geom vg, inputs;
327
327
# """
328
328
329
- # # Q3
330
- # point = f"ST_SetSRID(ST_Point({lon}, {lat}), 4326)"
331
- # main_query = f"""
332
- # SELECT
333
- # geom,
334
- # hwsd2,
335
- # ST_Distance(
336
- # geom::geography,
337
- # {point}::geography
338
- # ) AS distance,
339
- # ST_Intersects(geom, {point}) AS pt_intersect
340
- # FROM {table_name}
341
- # WHERE ST_DWithin(geom::geography, {point}::geography, {buffer_dist});
342
- # """
329
+ # Q3
330
+ point = f"ST_SetSRID(ST_Point({ lon } , { lat } ), 4326)"
331
+ main_query = f"""
332
+ SELECT
333
+ geom,
334
+ hwsd2,
335
+ ST_Distance(
336
+ geom::geography,
337
+ { point } ::geography
338
+ ) AS distance,
339
+ ST_Intersects(geom, { point } ) AS pt_intersect
340
+ FROM { table_name }
341
+ WHERE ST_DWithin(geom::geography, { point } ::geography, { buffer_dist } );
342
+ """
343
343
344
344
# Use GeoPandas to execute the main query and load results into a GeoDataFrame.
345
345
hwsd = gpd .read_postgis (main_query , connection , geom_col = "geom" )
0 commit comments