Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I want to use ST_GeomFromText() to load vector data to PostGIS. However, I don't know how to convert from Geojson type to WKT.

N.B. I know I can use OGR2OGR to upload the geojson directly, but I just want to confirm if there is any way to convert it or not. Thanks.

share|improve this question
    
ogr2ogr could be a way –  Below the Radar May 20 '13 at 17:39
    
Efficient in what way? –  R.K. May 21 '13 at 4:04
1  
If your PostGIS version >2, you import GeoJSON directly. –  Devdatta Tengshe May 21 '13 at 7:16

2 Answers 2

why not use

see Creating GeoJSON Feature Collections with JSON and PostGIS functions or ST_GeomFromGeoJSON from OpenGeo.

To convert to WKT, use

  • ST_AsText , the reverse of ST_GeomFromText() which return the Well-Known Text (WKT)
  • ST_AsEWKT same with SRID meta data
share|improve this answer
    
Hi, Thanks so much. I don't even know this tool. I am still learning from you as a student. –  Yu Guo May 21 '13 at 16:32

If Python is your thing then you can use GeoMet. It's a Python module that converts GeoJSON to WKT/WKB and vice versa. You can install it directly from the github repository using pip

$ pip install git+git://github.com/larsbutler/geomet.git

Here is a sample conversion:

>>> from geomet import wkt
>>> point = {'type': 'Point', 'coordinates': [116.4, 45.2, 11.1]}
>>> wkt.dumps(point, decimals=4)
'POINT (116.4000 45.2000 11.1000)'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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