Your GeoJSON sample lacks information about the coordinate reference system, although you say it is SRID 900913. This is so-called Google Mercator or Web Mercator, which has been superseded by EPSG:3857, which I will use in this example. An important question is whether you really want Well-Known Binary, which does not include an SRID, or Extended Well-Known Binary, which does. As PostGIS uses EWKB internally ST_GeomFromGeoJSON
will produce a geometry with the SRID, but since your test data does not include an SRID, the EWKB will be created with SRID of 0.
(In the code below I omit the lengthy GeoJSON for readability.)
So for example, the geometry can be converted with ST_GeomFromGeoJSON('...')
, but
SELECT ST_SRID(ST_GeomFromGeoJSON('...'));
returns 0
. If your data are in Web Mercator, and you tried to insert the result into a PostGIS table with a geometry column defined as Web Mercator, the insert would fail.
Nonetheless, if this is what you want (WKB, no embedded SRID), use:
SELECT ST_AsBinary(ST_GeomFromGeoJSON('...'));
If you want EWKB (WKB with an SRID), you have to use the ST_SetSRID
function to assign the correct ID. You would then return the EWKB using:
SELECT ST_AsEWKB(ST_SetSRID(ST_GeomFromGeoJSON('...'), 3857));
Note that since PostGIS geometries are all EWKB, the ST_AsEWKB
function is possibly unnecessary. But since you haven't specified the use I'm not sure if you would run into data type trouble if you skip it.
Finally, if, as you indicate in your comments to various proposed answers, you actually want the final result to be in EPSG:4326 (WGS 84 lat-long coordinates), you have to apply the ST_Transform
function after defining the SRID with ST_SetSRID
:
SELECT ST_AsEWKB(ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON('...'), 3857), 4326));