0

I have a dataframe with a column 'geometry', with either a MultiPolygon or a Polygon with this structure:

"{""coordinates"": [[[[7.550173037853544, 53.67503770572817], [7.547448703728663, 53.67578543283669], [7.546742188094777, 53.674936990272656], [7.544119530708709, 53.67559257263587], [7.539340754839757, 53.67377737266573]]]], ""type"": ""Polygon""}"

"{""coordinates"": [[[[7.550173037853544, 53.67503770572817], [7.547448703728663, 53.67578543283669], [7.546742188094777, 53.674936990272656], [7.544119530708709, 53.67559257263587], [7.539340754839757, 53.67377737266573]]]], ""type"": ""MultiPolygon""}"

(these are only parts of the entries, they would be too long to post).

Im trying to convert the whole DataFrame to a geodataframe, but im struggling.

What I did is delete all strings in the column and only leave the coordinates. Then I converted the columns to Polygons with this function:

def polygons_from_custom_xy_string(df_column):
    
    def chunks(lst, n):
        for i in range(0, len(lst), n):
            yield lst[i:i + n]
    
    def xy_list_from_string(s):
        # 'x y x y ...' -> [[x, y], [x, y], ...]
        return list(chunks([float(i) for i in s.split()], 2))
    
    def poly(s):
        """ returns shapely polygon from point list"""
        ps = xy_list_from_string(s)
        return Polygon([[p[0], p[1]] for p in ps])

    polygons = [poly(r) for r in df_column]


gemeinde['geometry'] = polygons_from_custom_xy_string(gemeinde['geometry'])
gemeinde = gpd.GeoDataFrame(gemeinde, geometry='geometry')

I can plot it like that, but all my MultiPolygons break. Is there an easier way to just convert all the shapes to the corresponding shape?

I also tried this, but it doesnt work:

gemeinde['geometry'] = gemeinde['geometry'].apply(wkt.loads)

with the error: WKTReadingError: Could not create geometry because of errors while reading input. Is my file broken? In all the other threads I saw that the ""type"": ""MultiPolygon"" part is at the beginning, not the end of the cell.

Any help is much appreciated, Im a bit lost here.

Elias
  • 51
  • 8
  • It looks like it’s geojson not wkt. Can you try parsing the geometry col with [`geopandas.GeoDataFrame.from_features`](https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoDataFrame.from_features.html)? If not, can you explain how you got here? Is this loaded from a file? – Michael Delgado Aug 03 '22 at 15:50
  • Does this answer your question? [Convert a column of GeoJSON-like strings to geometry objects in GeoPandas](https://stackoverflow.com/questions/60044524/convert-a-column-of-geojson-like-strings-to-geometry-objects-in-geopandas) – Michael Delgado Aug 03 '22 at 15:53
  • Oh okay, yes, I think I am using geojson. I still couldnt get it to work. Yes, it is loaded from a file, I got ir here: https://data.opendatasoft.com/explore/dataset/georef-germany-kreis%40public/export/?disjunctive.lan_code&disjunctive.lan_name&disjunctive.krs_code&disjunctive.krs_name&disjunctive.krs_name_short I loaded the csv file and merged it with some other infos that I want to plot. – Elias Aug 03 '22 at 17:33
  • Im getting the same error message as the guy in your thread, maybe I can implement his solution... geom_type = ob.get("type").lower() AttributeError: 'str' object has no attribute 'get' – Elias Aug 03 '22 at 17:40
  • thanks for the hint, you were right, I solved it. – Elias Aug 04 '22 at 07:27

1 Answers1

0

My data was a geojson file, not a wkt. I could convert my data to a geodataframe with this:

gemeinde['geometry'] = gemeinde['geometry'].apply(lambda x: geojson.loads(x))
gemeinde = gpd.GeoDataFrame(gemeinde, geometry='geometry')
Elias
  • 51
  • 8