Get startedGet started for free

GeoSeries attributes and methods II

1. GeoSeries attributes and methods II

Now we'll add to your knowledge of GeoSeries attributes and methods by looking at the centroid attribute and the distance method.

2. GeoSeries.centroid

Centroid returns the point at center of a geometry. We'll use the centroid property of the first geometry in the districts GeoDataFrame to find the point that is the center of that geometry, a polygon, shown in the illustration. The centroid is also plotted in the illustration as a black point.

3. School district centroids

Now we'll use the centroid attribute to find the point at the center of each school district polygon and store it in a new column.

4. School district centroids

We create a new column, called 'center', in the school_districts GeoDataFrame by assigning the school_district.geometry's centroid to it. And we'll look at part of school_districts to check our work - just the district number and center point.

5. GeoSeries.distance()

The distance method of a GeoSeries finds the minimum distance from the geometry it is called on to the geometry passed in as an argument labeled other. In this example the distance is being calculated from the red point to the centroid of the polygon (represented by the black point). Remember, distance units depend on the coordinate reference system.

6. Distance between two points

Let's find the distance to each school in school district one from the center of that school district. First, we will first create a subset of school_districts called district_one.

7. Distance between two points

Next we use the schools DataFrame, shown here, to create a GeoDataFrame, first creating the necessary geometry column. Now we can spatially join district_one with the schools.

8. Distance between two points

The spatial join we perform finds schools that are within district_one. We check the shape of the resulting GeoDataFrame and discover that there are 30 schools in district one.

9. Distance between two points

We're ready to calculate the distance between each school and the center_point of school_district_one. Remember the distances will be in decimal degrees. As we calculate the distance from the center of district one to each school in that district, we'll store it in a dictionary called distances. We use the iterrows() generator here to iterate through the schools_in_dist1 GeoDataFrame. We use the school name as the key and get the distance from each school's location (stored in the geometry column) to the center of the district (stored in the center column). We store the distance from the school to the center as the value for each school key in the dictionary. Here we use the pprint method of the pprint package to pretty print the first few rows of the distances dictionary.

10. Let's Practice!

Now it's your turn to explore what more you can do with a GeoSeries by working with the centroid attribute and the distance method.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.