Jkiefn1
Jkiefn1

Reputation: 143

Apple M1 Incompatibility - Creating a GeoJSON file from a DataFrame with Shapely Polygons

First note: My machine is an Apple M1, and GeoPandas is not supported on these machines yet, so while GeoPandas would be an easy go-to, it won't be an acceptable solution in this instance.

Is it possible to create a function or class to create a geojson file from an existing DataFrame, where the geometry is a polygon and is stored in a column?

I've extracted the contours of a custom map and managed to create shapely geometries of said contours. From there, I merged the geometries from the custom contours with a data frame of some information about the contours.

Below is an example of the structure of the DataFrame.

    ID   metric   geometry
0   Item_1   23   POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))
1   Item_2   17   POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))
2   Item_3   15   POLYGON ((0 0, 3 0, 3 3, 0 3, 0 0))

The solution found here looks like it would suffice for point coordinates, but there is no geojson.polygon attribute. At least not that I can find in the docs. Below is the solution; I've highlighted where its applicability breaks down.

import pandas as pd
import geojson

def data2geojson(df):
    features = []
    insert_features = lambda X: features.append(
            geojson.Feature(geometry=geojson.Point((X["long"],X["lat"],X["elev"])),

### I don't want to create a geoson.point geometry,    
### there is no such geojson.polygon attribute to point to my DataFrame's 'geometry' column of polygons,
### and I don't have the lats, longs, etc. due to the polygon being extracted from a custom contour.
### So the closest applicable solution breaks down at this point.

                            properties=dict(ID=X["ID"],
                                            metric=X["metric"])))
    df.apply(insert_features, axis=1)
    with open('map1.geojson', 'w', encoding='utf8') as fp:
        geojson.dump(geojson.FeatureCollection(features), fp, sort_keys=True, ensure_ascii=False)

data2geojson(df)

Thanks in advance

Upvotes: 0

Views: 460

Answers (1)

Jkiefn1
Jkiefn1

Reputation: 143

After un-installing and re-installing GeoPandas, Fiona, and PyProj the issue persisted. After some digging I found that leaving GeoPandas and PyProj installed, but uninstalling Fiona fixed the cyclical error and GeoPandas works as expected.

Upvotes: 1

Related Questions