Skip to content

Commit d1fbb60

Browse files
committed
feat: change query
1 parent 9d3eca4 commit d1fbb60

File tree

1 file changed

+51
-51
lines changed

1 file changed

+51
-51
lines changed

soil_id/db.py

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -257,45 +257,45 @@ def extract_hwsd2_data(connection, lon, lat, buffer_dist, table_name):
257257
# Distance is computed by casting geometries to geography,
258258
# which returns the geodesic distance in meters.
259259
# 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+
# ),
271271

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+
# )
281281

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+
# """
299299

300300
# # Q2
301301
# main_query = f"""
@@ -326,20 +326,20 @@ def extract_hwsd2_data(connection, lon, lat, buffer_dist, table_name):
326326
# FROM valid_geom vg, inputs;
327327
# """
328328

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+
"""
343343

344344
# Use GeoPandas to execute the main query and load results into a GeoDataFrame.
345345
hwsd = gpd.read_postgis(main_query, connection, geom_col="geom")

0 commit comments

Comments
 (0)