I have created a square grid and I would like to figure out the real world area of each polygon in a metric unit e.q. square metres via GeoDataFrame.area
.
import geopandas as gpd
from shapely.geometry import Polygon
# Latitude (y)
lat_min = -90
lat_max = 90
# Longtidue (x)
lon_min = -180
lon_max = 180
# Edge length
side_length = 0.5
# List to which we will append the polygons
temp_polygons = []
for y in range(int(abs(lat_min-lat_max)/side_length)):
for x in range(int(abs(lon_min-lon_max)/side_length)):
x_start = lon_min + x * side_length
y_start = lat_min + y * side_length
bottom_left_x = x_start
bottom_left_y = y_start
bottom_right_x = x_start + side_length
bottom_right_y = y_start
top_right_x = x_start + side_length
top_right_y = y_start + side_length
top_left_x = x_start
top_left_y = y_start + side_length
# Append polygon to list
temp_polygons.append(
# Polygon
Polygon([
# Bottom-left
(bottom_left_x,bottom_left_y),
# Bottom-right
(bottom_right_x,bottom_right_y),
# Top-left
(top_right_x,top_right_y),
# Top-right
(top_left_x,top_left_y)
])
)
# Turn list of polygons into GeoSeries
temp_polygons = gpd.GeoSeries(temp_polygons)
# Turn GeoSeries into GeoDataFrame
raster = gpd.GeoDataFrame({'geometry': temp_polygons})
# Add coordinate reference system
raster.crs = {"init":"epsg:4326"}
# Change projection
# https://en.wikipedia.org/wiki/Web_Mercator_projection#EPSG:3857
raster['geometry'] = (
raster['geometry'].to_crs({'init': 'EPSG:3857'})
)
# Get area
raster['area'] = raster.area
First, the geometries are as follows:
However, after applying ...
# Change projection
# https://en.wikipedia.org/wiki/Web_Mercator_projection#EPSG:3857
raster['geometry'] = (
raster['geometry'].to_crs({'init': 'EPSG:3857'})
)
... there are a lot of inf values and hence the cells' area is nan
:
What am I doing wrong? Did I select a wrong CRS? How can I determine the area of each grid cell in a metric unit?
Edit:
This answer seems to work for me. However, with ~25 iterations/second it's rather slow (~3 hours for ~260,000 rows). It's not prohibitively slow but there must be a quicker solution.