What is the correct way to store a Google Maps DrawingManager rectangle overlay? The API basically provides me with NE & SW lat/long coordinates. Should I use BOX()? Is it possible to create a BOX() from a GeoJSON object?
[GIS] How to store Google Maps DrawingManager Rectangle overlay in PostgreSQL Postgis GEOMETRY(GEOMETRY, 4326)
google mapsmap-drawingpostgispostgresql
Best Answer
At some point you'll have to convert from Google native overlays to WKT. You can either do it in your frontend, using for example Wicket.js or in your backend, using postgis's ST_FromGeoJson. Keep in mind that there's no geoJSON spec for rectangles. They're just polygons.
If you use Wicket.js you will be able to ingest a WKT BOX to generate a google.maps.Rectangle
However, for the sake of consistency, when you export it as WKT, you'll get the equivalent polygon:
This is because you could also want to export it as geoJSON and it wouldn't make sense to output a different object
So, you see, this wont work to export your Rectangles to boxes. But nothing stops you from extending the google.maps.Rectangle class
So you can do:
Keep in mind that in PostGIS, the Box2D type is a geometry by itself, as opposed to WKT that is just text. This means BOX2D is treated differently. For example:
Stores a geometry. whereas
Yields a string.
If you used
ST_GeomFromText
on a BOX string, it would result in an error.If you used
ST_AsText
in a BOX geometry, it would yield the equivalent polygon.This means you'll have to adapt your logic to apply the
Box2D
function both when storing and retrieving a box geometry.Disclaimer: I'm a humble contributor to Wicket.