1

I'm trying to convert a matplotlib contourf to geojson, but i'm stuck. I created contourf from a csv.

Here's my code :

data = np.genfromtxt('centroid.csv', delimiter=',')
x = data[..., 0]
y = data[..., 1]
z = data[..., 2]
xi = np.linspace(x.min(), x.max(), 200)
yi = np.linspace(y.min(), y.max(), 200)
zi = griddata(x, y, z, xi, yi)

nb_class = 3

collec_poly = plt.contourf(xi, yi, zi, nb_class, vmax=abs(zi).max(), vmin=-abs(zi).max())

geojson = geojsoncontour.contour_to_geojson(contour=collec_poly, contour_levels=nb_class, geojson_filepath='out.geojson', ndigits=3, unit='m')

it returns me this error :

assert len(contour_levels) == len(collections)
TypeError: object of type 'int' has no len()

I can't really understand all needed arguments for contour_to_geojson and I didn't find a good documentation.

Somebody can help me ?

0

If I use the same simple example as in Converting Matplotlib contour objects to Shapely objects

import matplotlib.pyplot as plt
x = [1,2,3,4]
y = [1,2,3,4]
m = [[15,14,13,12],[14,12,10,8],[13,10,7,4],[12,8,4,0]]
figure = plt.figure()
ax = figure.add_subplot(111)
contourf = ax.contourf(x,y,m, cmap=plt.cm.jet)
plt.show()

enter image description here

With basic geojsoncontour

import geojsoncontour
geojson = geojsoncontour.contourf_to_geojson(contourf=contourf)
d = json.loads(geojson)
for i in d['features']:
     print i

{u'geometry': {u'type': u'MultiPolygon', u'coordinates': [[[[4.0, 3.5], [4.0, 4.0], [3.5, 4.0], [4.0, 3.5]]]]}, u'type': u'Feature', u'properties': {u'stroke-opacity': 1, u'title': u'0.00 ', u'fill-opacity': 0.9, u'stroke': u'#0000c8', u'stroke-width': 1, u'fill': u'#0000c8'}}
{u'geometry': {u'type': u'MultiPolygon', u'coordinates': [[[[4.0, 3.0], [4.0, 3.0], [4.0, 3.5], [3.5, 4.0], [3.0, 4.0], [3.0, 4.0], [3.0, 4.0], [4.0, 3.0], [4.0, 3.0]]]]}, u'type': u'Feature', u'properties': {u'stroke-opacity': 1, u'title': u'2.00 ', u'fill-opacity': 0.9, u'stroke': u'#0040ff', u'stroke-width': 1, u'fill': u'#0040ff'}}
{u'geometry': {u'type': u'MultiPolygon', u'coordinates': [[[[4.0, 2.5], [4.0, 3.0], [4.0, 3.0], [3.0, 4.0], [3.0, 4.0], [2.5, 4.0], [3.0, 3.33333], [3.33333, 3.0], [4.0, 2.5]]]]}, u'type': u'Feature', u'properties': {u'stroke-opacity': 1, u'title': u'4.00 ', u'fill-opacity': 0.9, u'stroke': u'#00c0ff', u'stroke-width': 1, u'fill': u'#00c0ff'}}
{u'geometry': {u'type': u'MultiPolygon', u'coordinates': [[[[4.0, 2.0], [4.0, 2.0], [4.0, 2.5], [3.33333, 3.0], [3.0, 3.33333], [2.5, 4.0], [2.0, 4.0], [2.0, 4.0], [2.0, 4.0], [2.66667, 3.0], [3.0, 2.66667], [4.0, 2.0], [4.0, 2.0]]]]}, u'type': u'Feature', u'properties': {u'stroke-opacity': 1, u'title': u'6.00 ', u'fill-opacity': 0.9, u'stroke': u'#49ffad', u'stroke-width': 1, u'fill': u'#49ffad'}}
{u'geometry': {u'type': u'MultiPolygon', u'coordinates': [[[[3.0, 2.0], [4.0, 1.5], [4.0, 2.0], [4.0, 2.0], [3.0, 2.66667], [2.66667, 3.0], [2.0, 4.0], [2.0, 4.0], [1.5, 4.0], [2.0, 3.0], [2.0, 3.0], [3.0, 2.0], [3.0, 2.0]]]]}, u'type': u'Feature', u'properties': {u'stroke-opacity': 1, u'title': u'8.00 ', u'fill-opacity': 0.9, u'stroke': u'#b1ff46', u'stroke-width': 1, u'fill': u'#b1ff46'}}
{u'geometry': {u'type': u'MultiPolygon', u'coordinates': [[[[4.0, 1.0], [4.0, 1.0], [4.0, 1.5], [3.0, 2.0], [3.0, 2.0], [2.0, 3.0], [2.0, 3.0], [1.5, 4.0], [1.0, 4.0], [1.0, 4.0], [1.33333, 3.0], [2.0, 2.0], [2.0, 2.0], [3.0, 1.33333], [4.0, 1.0]]]]}, u'type': u'Feature', u'properties': {u'stroke-opacity': 1, u'title': u'10.00 ', u'fill-opacity': 0.9, u'stroke': u'#ffd000', u'stroke-width': 1, u'fill': u'#ffd000'}}
{u'geometry': {u'type': u'MultiPolygon', u'coordinates': [[[[2.0, 1.0], [2.0, 1.0], [3.0, 1.0], [4.0, 1.0], [3.0, 1.33333], [2.0, 2.0], [2.0, 2.0], [1.33333, 3.0], [1.0, 4.0], [1.0, 3.0], [1.0, 2.0], [1.0, 2.0], [2.0, 1.0]]]]}, u'type': u'Feature', u'properties': {u'stroke-opacity': 1, u'title': u'12.00 ', u'fill-opacity': 0.9, u'stroke': u'#ff5900', u'stroke-width': 1, u'fill': u'#ff5900'}}
{u'geometry': {u'type': u'MultiPolygon', u'coordinates': [[[[1.0, 1.0], [2.0, 1.0], [1.0, 2.0], [1.0, 1.0]]]]}, u'type': u'Feature', u'properties': {u'stroke-opacity': 1, u'title': u'14.00 ', u'fill-opacity': 0.9, u'stroke': u'#c40000', u'stroke-width': 1, u'fill': u'#c40000'}}

Now you can modify:

1) the contour or contourf arguments as in your example
2) the geojson properties elements in the declaration

geom = [i for i in  d['features']]
geom[4]['properties']['stroke-width']
1
# modify stroke_with for example
geojson = geojsoncontour.contourf_to_geojson(contourf=contourf,stroke_width=5)
d = json.loads(geojson)
geom = [i for i in  d['features']]
geom[4]['properties']['stroke-width']
5

You can also use tricontour and tricontourf

import geopandas
# read point shapefile
points = gpd.read_files("points.shp")
points['x'] = points.geometry.apply(lambda x: x.x)
points['y'] = points.geometry.apply(lambda x: x.y)
# contouring x,y, z with tricontourf
cnt = ax.tricontourf(points.x, points.y,points.elev)
geojson = geojsoncontour.contourf_to_geojson(contourf=cnt, unit="m")
contours = json.loads(geojson)
# create resulting shapefile
result  = gpd.GeoDataFrame.from_features(contours['features'])
result.to_file("contours.shp")

enter image description here

  • I tried to follow your post but I'm stuck with geojson = geojsoncontour.contourf_to_geojson(contourf=contourf) It returns me AttributeError: 'module' object has no attribute 'contourf_to_geojson' – Tim C. Jun 11 '18 at 8:57
  • Look at geojsoncontour: example_contourf.py – gene Jun 11 '18 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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