I have an shp. with these columns:

ind code area
1    12  200
1    12  100
1    12  23
2    30  223
2    30  112
3    29  234
4    20  232
5    23  211

I want to combine each polygon with the same value in the ind column as one.

what I think is needed is a merge operation but how should I do it? Also will the area be summed of the polygons that will be one? for example will we have one row of ind=1 that its area will be 323?

The examples with the merge are like this and involve two shp 's: how should I do with one shp?

# One GeoDataFrame of countries, one of Cities.
# Want to merge so we can get each city's country.
In [11]: countries.head()
                                            geometry               country
0  POLYGON ((61.21081709172574 35.65007233330923,...           Afghanistan
1  (POLYGON ((16.32652835456705 -5.87747039146621...                Angola
2  POLYGON ((20.59024743010491 41.85540416113361,...               Albania
3  POLYGON ((51.57951867046327 24.24549713795111,...  United Arab Emirates
4  (POLYGON ((-65.50000000000003 -55.199999999999...             Argentina

In [12]: cities.head()
           name                                     geometry
0  Vatican City  POINT (12.45338654497177 41.90328217996012)
1    San Marino    POINT (12.44177015780014 43.936095834768)
2         Vaduz  POINT (9.516669472907267 47.13372377429357)
3    Luxembourg  POINT (6.130002806227083 49.61166037912108)
4       Palikir  POINT (158.1499743237623 6.916643696007725)

# Execute spatial join
In [13]: cities_with_country = gpd.sjoin(cities, countries, how="inner", op='intersects')

I think you want to take a look at the dissolve method, where you can group by a certain column, take the union of the geometries for each group, and do another aggregation method on the other columns.
For example, with the countries example:

continents = countries.dissolve(by='continent', aggfunc='sum')

or in your case:

gdf.dissolve(by='ind', aggfunc='sum')
  • I think you are correct I a have realized that it was dissolve and the output was nice but the sum u performed eluded me.Thanks for the addition. I will check it out and hope it works this part. – user122244 Jun 19 '18 at 12:03
  • It says it can't happen. says type error and mentions a certain column. How is it supposed to sum the area column?it checks the numeric columns that will be dissoved and sums them? – user122244 Jun 19 '18 at 12:09
  • Yes, it should take the sum for the numerical columns (and ignore the non-numerical ones). Can you provide the full traceback and example code? – joris Jun 19 '18 at 12:19

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.