find_points test added.
This commit is contained in:
parent
158d83341b
commit
0c07d7ea93
BIN
__pycache__/main.cpython-311.pyc
Normal file
BIN
__pycache__/main.cpython-311.pyc
Normal file
Binary file not shown.
BIN
__pycache__/routes.cpython-311.pyc
Normal file
BIN
__pycache__/routes.cpython-311.pyc
Normal file
Binary file not shown.
39
routes.py
39
routes.py
|
@ -1,23 +1,40 @@
|
||||||
import os
|
import os
|
||||||
from flask import jsonify
|
from flask import jsonify, request
|
||||||
from sqlalchemy import inspect
|
from sqlalchemy import inspect, func, text
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
from geoalchemy2 import Geometry
|
||||||
|
from shapely import wkb
|
||||||
|
|
||||||
def register_route(app,db):
|
def register_route(app,db):
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def hello_world():
|
def hello_world():
|
||||||
return "<p>Hello, World!</p>"
|
return "<p>Hello, World!</p>"
|
||||||
|
|
||||||
@app.route('/user/')
|
|
||||||
@app.route('/user/<username>')
|
|
||||||
def show_user_profile(username=None):
|
|
||||||
test = os.getenv("MY_KEY")
|
|
||||||
if username:
|
|
||||||
return f"<p>Hello, Mr {username}! You are the man.</p> {test}"
|
|
||||||
return "<p>Hello, user!</p>"
|
|
||||||
|
|
||||||
@app.route('/table')
|
@app.route('/table')
|
||||||
def list_table():
|
def list_table():
|
||||||
inspector = inspect(db.engine)
|
inspector = inspect(db.engine)
|
||||||
tables = inspector.get_table_names()
|
tables = inspector.get_table_names()
|
||||||
return jsonify(tables)
|
return jsonify(tables)
|
||||||
|
|
||||||
|
@app.route('/find_points/', methods=['POST'])
|
||||||
|
def find_points():
|
||||||
|
data = request.json
|
||||||
|
coordinates = data.get('coordinates', [])
|
||||||
|
polygon_wkt = 'POLYGON(({}))'.format(', '.join(f'{lat} {lng}' for lat, lng in coordinates))
|
||||||
|
|
||||||
|
# Construct the query string
|
||||||
|
query = f"""
|
||||||
|
SELECT id, geom FROM agents
|
||||||
|
WHERE ST_Contains(
|
||||||
|
ST_Transform(ST_GeomFromText('{polygon_wkt}', 4326), 4326),
|
||||||
|
geom
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
|
||||||
|
with db.engine.connect() as connection:
|
||||||
|
results = connection.execute(text(query)).fetchall()
|
||||||
|
|
||||||
|
# Convert results to a list of dictionaries for JSON serialization
|
||||||
|
results_list = [{"id": result.id, "geom": [wkb.loads(bytes.fromhex(result.geom)).x, wkb.loads(bytes.fromhex(result.geom)).y]} for result in results]
|
||||||
|
|
||||||
|
return jsonify(results_list)
|
Loading…
Reference in New Issue
Block a user