2

I would like to add columns xmin, xmax, ymin, ymax and I take coordinates from column 'geometry'. I have exemplary dataframe with 1 records. Where is the problem?

Data:

   FID_1        GODLO                                           geometry
0      0  7.101.01.21  POLYGON ((7340000.000 5425000.000, 7340000.000...

I use, but I am getting the error:

TypeError: 'Polygon' object does not support item assignment
import geopandas as gpd

budynki = gpd.read_file('/home/gis/siatki_skorowidzowe/s7_2000/2000s7_2000.shp', rows = slice(0,1), encoding ='cp1250')

xlist = []
ylist = []
for polygon in budynki['geometry']:
    for x, y in polygon.exterior.coords:
        xlist.append(x)
        ylist.append(y)
        for x in xlist:
            minx = float(min(xlist))
            maxx = float(max(xlist))
        for y in ylist:
            miny = float(min(ylist))
            maxy = float(max(ylist))
    polygon['xmin'] = minx
    polygon['xmax'] = maxx
    polygon['ymin'] = miny
    polygon['ymax'] = maxy
1
  • 1
    Perhaps change polygon['xmin'] = minx to budynki['xmin'] = minx as a start. Your 'polygon' variable is not a row of your dataframe, it's a shapely polygon. So you can't assign it columns. This will still not be right, though, as you haven't defined the 'xmin', 'xmax', etc columns in your dataframe. And which row of the dataframe should they go in? Your code needs to address that. – Jon Feb 18 at 15:00
4

polygon is a shapely geometry in your script, not a row. So you cannot add a key as in a dictionary or a column as in (geo)pandas (Geo)DataFrame.

Use this script.


# other lines

def get_bounds(r):
    minx, miny, maxx, maxy =  r["geometry"].bounds    
    r["minx"] = minx
    r["miny"] = miny
    r["maxx"] = maxx
    r["maxy"] = maxy
    return r
    
budynki = budynki.apply(get_bounds, axis=1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.