user2315860
user2315860

Reputation:

Draw polygon on google map and query item located in that area

I have a web-based dashboard which is populated with lon-lat information of vechicle movements over 7 days. The map layer in this case is Google map and data for the vehicle movement is stored in PostgreSQL. What user would like to do is to draw a polygon on a specific area, and trigger a query that will show all occurrences of vehicles that have passed that area. I already have the map and all vehicles lon lat data, is there a way I can do this using json or javascript without doing uploading the polygon to the database?

I am aware that Postgress can do this if I have a polygon uploaded into the database. For security and data management I don't want to upload the polygon into PostgreSQL. I want to draw the polygon and trigger the query using JavaScript.

Upvotes: 2

Views: 467

Answers (1)

geozelot
geozelot

Reputation: 567

Just use a static geometry like

SELECT *
FROM <points_table> AS pt
WHERE ST_Intersects(pt.geom, <static_geometry);

where <static_geometry> would be one of

and parse the transferred input coordinates into the function (note the different coordinate order for each function). No upload needed, still, since those functions accept only string formatted input, take the usual precautions for possible SQL injections.
There are equivalent functions for geometry output (ST_As<Format>, e.g. ST_AsGeoJSON).

How to best collect, transfer and parse the polygon coordinates from client to server to DB is up to your setup.

Upvotes: 1

Related Questions