6

While there are functions to convert geography to GeoJSON:

text ST_AsGeoJSON(geography geog, integer maxdecimaldigits=15, integer options=0);

I do not see any way to go the opposite direction (GeoJSON to geography) only to geometry. http://postgis.net/docs/reference.html#Geometry_Constructors

Is this on the roadmap?


Update:

The data in the database has geography polygons or multi-polygons to describe each item in the DB. The query is coming from lealfet.js, which allows me to easily get the shape in GeoJSON.

What I am doing is just a simple ST_Intersects() to check for overlap and return. Yes, I have a hack to convert this to EWKT right now, but I was wondering if it was on the roadmap to support this in a future version of PostGIS, or where one adds it as a wish list (since symmetry is a good thing, and it seems a shame that only geometry is supported in that direction).

  • 1
    You're not asking how to (use which nested functions)? Just is the single function solution coming? – Martin F Mar 4 '14 at 3:14
  • Is your objective to take GeoJSON data from flat files (i.e. outside of the database) and put them into your database? If so, are you aware of the ogr2ogr and shp2pgsql utilities? – Arthur Mar 4 '14 at 14:00
  • I added an update to address both these questions. Did I answer them? – Dr.YSG Mar 5 '14 at 15:12
7

Why don't you just do:

SELECT ST_GeomFromGeoJSON(geojsongoeshere)::geography;
  • Are you saying that ST_GeogFromGeoJSON is the same as: ST_GeogFromWKB(ST_GeomFromGeoJSON(...)))? [assume the JSON is only polygons in EPSG 4326 format] – Dr.YSG Mar 18 '14 at 14:48
  • 1
    If you are using a different projection than 4326 for geo json, then you would do: SELECT ST_Transform(ST_GeomFromGeoJSON(geojsongoeshere),4326)::geography; I wouldn't use ST_GeogFromWKB because geometry is technically NOT WKB. It'll work but geography(geometry) also written as geometry::geography is a more direct conversion. – LR1234567 Mar 20 '14 at 0:09
  • I am keeping in all in 4326. Thank you for your expertise. – Dr.YSG Mar 20 '14 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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