[GIS] Convert WKT (or geojson) geometry to numpy array of coordinates

geojsonnumpypythonwell-known-text

I am looking for the fastest way to convert WKT or geojson geometry from string to numpy array of coordinates.

Is it possible to do it strictly in Python and without creating geometry object such Shapely shape, OGRGeometry or GEOSGeometry?

Best Answer

Converting GeoJson to numpy array seems to be much easier than trying to convert WKT to numpy array.

import numpy as np
import json

#if your geojson is a string
geoJson = json.load(geoJsonString)

numpy_array = np.array(geoJson['coordinates'])

and back to geojson:

geoJson['coordinates'] = numpy_array.tolist()

#if you are looking for string geojson
geoJsonString = json.dumps(geoJson)

That's all for geojson. Please answer the question if you have a solution for WKT