Skip to content

Commit

Permalink
Fixed issue with query
Browse files Browse the repository at this point in the history
Signed-off-by: Andrii Malchyk <snooki17@gmail.com>
  • Loading branch information
AMProduction committed Oct 9, 2023
1 parent ff21e18 commit 4fc8544
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions geoapi/src/db_tools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from sqlalchemy import Table, MetaData, select
from sqlalchemy import create_engine
from sqlalchemy import text

Expand All @@ -16,8 +15,6 @@ def get_area_db(region: str) -> float:
""")
results = conn.execute(sql)
return results.first()[0]
# for row in results:
# return row[0]


def get_gross_yield_db(region: str) -> float:
Expand All @@ -32,8 +29,6 @@ def get_gross_yield_db(region: str) -> float:
""")
results = conn.execute(sql)
return results.first()[0]
# for row in results:
# return row[0]


def get_weighted_average_yield_per_hectare_db(region: str) -> float:
Expand All @@ -49,20 +44,18 @@ def get_weighted_average_yield_per_hectare_db(region: str) -> float:
""")
results = conn.execute(sql)
return results.first()[0]
# for row in results:
# return row[0]


def get_nearby_fields_db(x: float, y: float, distance: int):
with engine.connect() as conn:
# initialize the Metadata Object
meta = MetaData()
# create a table schema
france = Table('france', meta, schema='france', autoload_with=engine)
# # initialize the Metadata Object
# meta = MetaData()
# # create a table schema
# france = Table('france', meta, schema='france', autoload_with=engine)
sql = text(f"""
Select *
FROM {os.getenv('DB_SCHEMA_NAME')}.{os.getenv('DB_TABLE_NAME')} as fr
WHERE ST_DWithin(fr.wkb_geometry::geography, (ST_SetSRID(ST_MakePoint({x}, {y}, 4326))::geography, {distance});
WHERE ST_DWithin(fr.wkb_geometry::geography, (ST_SetSRID(ST_MakePoint({x}, {y}), 4326))::geography, {distance});
""")
result = conn.execute(sql)
return result.fetchall()

0 comments on commit 4fc8544

Please sign in to comment.