3

I am trying to insert GeoJSON polygon features into a PostGIS DB. The input coordinate points are only the left-top corner and the bottom right corner. I have tried to use the ST_MakeEnvelope, but it is not working.

CREATE TABLE geojsonpoly (
    id serial PRIMARY KEY,
    name VARCHAR(64),
    polygon GEOMETRY
);


insert into geojsonpoly(name,polygon)
values('poly1',ST_SetSRID(
ST_GeomFromGeoJSON('
{"type":"Polygon",
"coordinates":[
[
ST_MakeEnvelope(
-114.017347,51.048005,
-114.005899,51.045381
)
]
]}
'),4326));

The only part I am struggling with the the "coordinates": where I need to use any method that can return a rectangular polygon from two points.

1 Answer 1

6

PostGIS has no concept of GeoJSON - except for the I/O functionality, which translates between the internal and the GeoJSON representation of geometric features. Likewise does your exemplary input not look like a GeoJSON representation.

If the cell maxima are in fact simple pairs of coordinates, you'd simply need to rearrange them to fit the xmin,ymin,xmax,ymax order (i.e. derived from the bottom-left & top-right corners) of ST_MakeEnvelope:

CREATE TABLE geojsonpoly (
    id     INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    "name" VARCHAR(64),
    geom   GEOMETRY(POLYGON, 4326)
);

INSERT INTO
  geojsonpoly ("name", geom)
VALUES
  ( 'poly1', ST_MakeEnvelope(<xmin>, <ymin>, <xmax>, <ymax>, 4326) )
;
2
  • many thanks i was a bit confused as the concept is new for me. that worked perfectly for me.
    – user9995
    Feb 22, 2023 at 9:15
  • @user9995 glad it helped. Consider accepting this answer then to let other users know it solved your problem.
    – geozelot
    Feb 27, 2023 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.