1

I have imported a shape file of Australian local government areas (LGA's) into google Colab and successfully read into geopandas. The subsequent geodataframe

lga_df = gpd.read_file("LGA_2016_AUST.shp")

has a geometry field with a list of polygons. I am trying to find the centroid / lat & long point in order to use the LGA names as labels in the map I will create.

The code I am using to create the centroid field is

 lga_df["center"] = lga_df["geometry"].centroid
 lga_df_points = lga_df.copy()
 lga_df_points.set_geometry("center", inplace = True)

Logic is

copy the original df_lga to a new df, and then set the geometry column to the newly created center points column because a GeoPandas df can only have one geometry column)

However, I am getting the following message

AttributeError: 'NoneType' object has no attribute 'centroid'. 

I have also tried code using representative_point() but doesn;t work either

I believe I have imported all the necessary dependencies - (see below)

import geopandas as gpd
from geopandas import GeoDataFrame
import pandas as pd 
import matplotlib.pyplot as plt
%matplotlib inline
from shapely.geometry import Point, LineString
import adjustText as aT

I have tried for a few hours but still get same error. Can someone advise where I am going wrong


Update - This Problem has been solved - geometry field had rows with No polygon co-ordinates. I have removed the rows without polygon coordinates. However, another error is thrown out. "Attribute error - Series does not have the attribute 'centroid'. I am thinking for some reason python is not recognising my geometry field as a geometry field but text / string field. How do I convert a geometry field that is a string to a geometry field?

1 Answers1

3

Try keeping the name of the geometry field:

lga_df_points = lga_df.copy()
lga_df_points["geometry"] = lga_df_points["geometry"].centroid

I don't know why it works but it worked for me. I guess that there is a bug changing the name of the geom field

  • 1
    It is actually because geopandas/shapely cannot save multiple geometries. But that's the best work around – Fjord Mar 27 '21 at 18:17