3

I'm undertaking a fairly simple workflow all within a python script with the goal of passing urlencoded GeoJSON in a URL to the Mapbox static API:

  1. create a point from DD string in EPSG:4326:
  2. convert to UTM for easier units and buffer the point generating a polygon
  3. transform back to WGS84 for the API
  4. generate valid, URL encoded GeoJSON string.

My hangup is the last part. I've used Shapely and pyproj to conduct 1-3 but using:

json.dumps(mapping(<shapely.geometry.polygon.Polygon at 0x106a22690>)) (and several permutations of this) and encoding it using urlencode or url_quote generates an encoded URL with JSON that returns 'Invalid Geojson' from Mapbox.

I am optimistic that the geojson library may solve this, but I can't seem to find a simple way to convert a shapely Polygon object to a GeoJSON polygon object.

5
  • Without a real example, we can do nothing an json.dumps(mapping(<shapely.geometry.polygon.Polygon at 0x106a22690>)) is not a correct formulation
    – gene
    Oct 10, 2016 at 18:05
  • apologies and update: geojson.dumps(mapping(poly)) where poly is a valid shapely polygon geometry generates valid geojson. my challenge at this point is to encode it in such a way that the mapbox api doesn't reject it.
    – PeterT
    Oct 10, 2016 at 18:17
  • look at Mapbox: Add a GeoJSON polygon
    – gene
    Oct 10, 2016 at 18:29
  • thanks @gene I'm working with the static API. just tried the mapbox-sdk-py as well and get a 422 server response with valid python geojson object... this feels like it should be easy. I must be missing something...
    – PeterT
    Oct 10, 2016 at 19:40
  • gis.stackexchange.com/questions/41465/… check this link out
    – ziggy
    Oct 10, 2016 at 22:03

1 Answer 1

2

Decided to use the mapbox-sdk-py library instead. Works well but the pipeline from shapely to a map-able feature using the SDK is a little cumbersome (per this issue)

Need to add some features to the shapley (geom, below) object and encapsulate it in a list:

feature = [{'type': 'Feature', 'properties': {}, 'geometry': mapping(geom)}]

for the Static method to work.

Thanks @perrygeo and @sgillies for the hints.

1
  • What way is to avoid sequence of tuples in coordinates? It returns in mapping(geom): ` 'coordinates': ((37.353705999999995, 55.98468299999999), (37.353929229834065, 55.98471677959936),...` I would like the native sequence of lists instead
    – Jane
    Nov 21, 2019 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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