HiveBrain v1.2.0
Get Started
← Back to all entries
debugsqlMinor

ERROR: invalid GeoJson representation

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
invaliderrorrepresentationgeojson

Problem

the GeoJSON Spec show,

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
}


But when I wrap that in a call to ST_GeomFromGeoJSON, like this,

SELECT ST_GeomFromGeoJSON(${                                                                                                                          
  "type": "Feature",                                                                                                                                   
  "geometry": {                                                                                                                                        
    "type": "Point",                                                                                                                                   
    "coordinates": [125.6, 10.1]                                                                                                                       
  },                                                                                                                                                   
  "properties": {                                                                                                                                      
    "name": "Dinagat Islands"                                                                                                                          
  }                                                                                                                                                    
}$);


I get the error,


ERROR: invalid GeoJson representation

Solution

From the docs on ST_GeomFromGeoJSON


ST_GeomFromGeoJSON works only for JSON Geometry fragments. It throws an error if you try to use it on a whole JSON document.

This means you'll want to cut out just the value from the geometry in your GeoJSON document,

SELECT ST_AsText(geom)
FROM ST_GeomFromGeoJSON(${                                                                                                                          
  "type": "Point",                                                                                                                                   
  "coordinates": [125.6, 10.1]                                                                                                                       
}$) AS geom;

     st_astext     
-------------------
 POINT(125.6 10.1)


ST_GeomFromGeoJSON is the inverse of ST_AsGeoJSON (which only produces the geometry section of GeoJSON document.)

Code Snippets

SELECT ST_AsText(geom)
FROM ST_GeomFromGeoJSON($${                                                                                                                          
  "type": "Point",                                                                                                                                   
  "coordinates": [125.6, 10.1]                                                                                                                       
}$$) AS geom;

     st_astext     
-------------------
 POINT(125.6 10.1)

Context

StackExchange Database Administrators Q#227580, answer score: 6

Revisions (0)

No revisions yet.