I've collected polygon data using ODK's geo_shape. I'm trying to convert it to a geojson for use in QGIS. I'm using python 3, pandas and geojson to do this.

The geo_shape output from ODK looks like this:

51.781223 -1.2891759999999999 0.0 0.0;51.78472...

Coordinates for each vertex contain two extra values (0.00 0.00) that are not needed (z value and accuracy I believe) and the x and y are the wrong way around. Therefore I've put together an ugly method of reshaping each feature's coordinates to something more useable. The processed geo_shape coordinates look like this:

[[-1.2891759999999999, 51.781223], [-1.266689, 51.784727], [-1.2407679999999999, 51.782497]]

and are added to a new column in the dataframe, 'myGeom':

result = [reshape_geom(x) for x in df_poly['habitat_area']]
df_poly['myGeom'] = result

Using the processed coordinates and this question I've got very close.

However, the resulting geojson will not draw in QGIS3 unless the coordinate list is enclosed in 4 square brackets [[[[my_x_1, my_y_1],[my_x_2, my_y_2],[my_x_3,my_x_3]]]].

The code looks like this:

features = df_nona.apply(
lambda row: Feature(geometry=MultiPolygon(row['myGeom'])),axis=1).tolist()

properties = df_nona.drop(['columns to drop'], axis=1).to_dict('records')
feature_collection = FeatureCollection(features=features, properties=properties)

with open('file06.geojson', 'w', encoding='utf-8') as f:
    json.dump(feature_collection, f, ensure_ascii=False)
  • can you put the geo_shape output? – Elio Diaz Sep 11 at 18:15

Your Answer

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

Browse other questions tagged or ask your own question.