Python GeoJSON – Convert Geometry to New Coordinate System and WKB Format

geojsonpythonshapelywell-known-binary

I have a GeoJSON like such:

{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "properties": {
            "uuid": "68F15C01-20CD-4D77-954B-9483BA1D4D33",
            "name": "ABC",
            "len": "529",
            "infotype": "Restricted",
            "h1": null,
            "join": null
        },
        "geometry": {
            "type": "LineString",
            "coordinates": [
                [-360909.60698310119, 7600968.922204642, 0.0],
                [-361357.344715965, 7600811.951385159, 0.0],
                [-361805.08159795138, 7600654.939420643, 0.0]
            ]
        }
    }]
}

I need to convert the geometry to EPSG:27700, as I've just realised I have no idea what the coordinate system above is supposed to be but I do know the placement of the features should be in the UK.

Once I've successfully done that, I'd then like the different feature geometries in WKB format using Python.
I've tried the below code but I'm getting the error:

ValueError: could not convert string to float: '-360909.60698310117, 7600968.922204642, 0.0, -361357.344715965, 7600811.951385159, 0.0, -361805.08159795136, 7600654.939420643, 0.0'
# Python program to read
# json file


import json
import shapely
from shapely import wkb, wkt
from shapely.geometry import Point

# Opening JSON file
f = open('metadata.json')


data = json.load(f)


for i in range(len(data['features'])):
    geom = str(data['features'][i]['geometry']['coordinates'])
    newstr = geom.replace("[", "")
    newstr2 = newstr.replace("]","")
    geom3 = float(newstr2)
    g = wkb.dumps(geom3, hex=True, srid=4326)

    print(g)

Best Answer

The coordinate system of GeoJSON may be EPSG:3857. You can use the following script

import json
import shapely
from shapely import wkb, wkt
from shapely.geometry import LineString

# Opening JSON file
f = open('metadata.json')
data = json.load(f)

for feat in data["features"]:
    geom = LineString(feat['geometry']['coordinates'])
    
    g = wkb.dumps(geom, hex=True, srid=3857)
    
    print(g)

# OUT:
# 01020000A0110F00000300000063FA8C6D360716C19E66053BD2FE5C410000000000000000D038FD60350E16C1947EE3FCAAFE5C410000000000000000D2698E53341516C1C3771FBC83FE5C410000000000000000

enter image description here

Related Question