1. GeoSeries attributes and methods I
So far you've learned to read in a file with the geopandas read_file() method and spatially join two GeoDataFrames with the sjoin() method. You've seen how methods that work with pandas DataFrames also work with GeoDataFrames. Now we'll explore some of the methods and attributes of a geopandas GeoSeries.
2. Shapely attributes and methods
You can think of a GeoSeries as the geometry column of a GeoDataFrame. Geopandas inherits a number of useful methods and attributes from the Shapely package. I'm going to tell you about three of them.
The area attribute returns the calculated area of a geometry.
The centroid attribute returns the center point of a geometry.
And the distance method gives the **minimum** distance from a geometry to a location specified using the `other` argument.
3. GeoSeries.area
We will look first at the area attribute of a GeoSeries. This illustration shows the first geometry, a polygon, in a GeoDataFrame called districts.
We can print the area of that polygon by printing the area attribute of the first geometry in districts. The units for area depend on the distance units for the coordinate reference system that the GeoSeries is using.
4. School district areas
We can see this in practice with the school_districts.
Here are the first 5 rows of the school_districts GeoDataFrame.
And remember there are just 9 school districts in total.
5. School district areas
We'll find the area of each school district with the GeoSeries area attribute. We'll store the areas as district_area and then print the sorted district areas to show the row index along with the area for each school district.
Here we see that the first row - school district 1 - is the largest school district.
Recall that the distance unit for a geometry is dependent on its coordinate reference system. The coordinate reference system for the school_district data is EPSG:4326, which uses decimal degrees for distance. So the area units here are decimal degrees squared.
Let's find the area in a way that is a little more comprehensible: kilometers squared.
6. School district areas
We can change the CRS to one that uses meters for distance and then convert meters squared to kilometers squared.
We create a GeoDataFrame, school_districts_3857 from the school_districts by using the to_crs() method and epsg:3857, a coordinate reference system that uses meters for distance.
Since there are 10^6 meters squared in one kilometer squared, we define a variable to help with the conversion and then find the area of each district and divide it by that conversion variable.
This time we see the first school district has an area of 563 km squared.
7. Let's Practice!
Before we learn about the centroid attribute and distance method, let's go practice working with the GeoSeries area attribute.