# Calculate area, length, width of rectangles in shapefile with Python

I have a list of rectangles imported from a shapefile into Python in degrees. (EPSG:4326)

``````rectangles = [{'type': 'Polygon',
'coordinates': [[(-61.55002968237319, 73.546895599812856),
(-61.55006750140465, 73.54690126492163),
(-61.55004657065852, 73.547002738155776),
(-61.55000884141658, 73.546996653057328),
(-61.55002968237319, 73.546895599812856)]]},
{'type': 'Polygon',
'coordinates': [[(-61.55006750140465, 73.54690126492163),
(-61.55010523064657, 73.54690693003006),
(-61.55008438977398, 73.547007961258084),
(-61.55004657065852, 73.547002738155776),
(-61.55006750140465, 73.54690126492163)]]},
{'type': 'Polygon',
'coordinates': [[(-61.55010523064657, 73.54690693003006),
(-61.55014304972003, 73.546912595138153),
(-61.55012211897393, 73.547013646160064),
(-61.55008438977398, 73.547007961258084),
(-61.55010523064657, 73.54690693003006)]]}]
``````

What I would like to do is calculate the area of each rectangle in square meters, and then the length and width of each rectangle in meters. There is no guarantee that the coordinates are in the same order for every rectangle.

What I would like to do is output a Python list for each metric, for example

``````area = [10.933836, 10.920366, 10.872225]
length = [4.721, 4.703, 4.725]
width = [2.316, 2.322, 2.301]
``````

(Those are not the actual dimensions from the coordinates above, just an example of the output format.)

I tried using GeoPandas, but when I try to convert from EPSG:4326 to any other projection, the Python session hangs and doesn't complete, even for small files with only 3 shapes. For example, I tried

``````import geopandas as gpd
test = gpd.read_file('test.shp')
print(test.crs)
> {'init': 'epsg:4326'}

test = test.to_crs({'proj':'cea'})
``````

The last line never completes, even for small files, I'm not sure why.

What are some approaches I can use to calculate the area, length, width for multiple shapes and output to lists in Python?

## 1 Answer

To make the `test.to_crs()` function work use as input the corresponding proj4 string, i.e.:

``````test = test.to_crs("+proj=cea +lon_0=-117.333333333333 +lat_ts=33.75 +x_0=0 +y_0=0 +ellps=clrk66 +datum=NAD27 +units=m +no_defs")
``````
• Same problem, it hangs and Python never completes the command, even for only 3 shapes. There may be something wrong with my installation, is there another method you would suggest outside of geopandas? – jua-kali Jul 10 at 20:38
• I tried this line of code in my PC using a set of points (in WGS84) as a test dataset and worked fine. An an alternative, you can try GDAL (see here: gis.stackexchange.com/questions/264618/…); or fiona+shapely (see here: gis.stackexchange.com/questions/265589/…); or do it manually in QGIS and then import the reprojected shapefile. – pan Jul 11 at 5:34