Geopandas from_feature function failing for MultiPolygon

86 views Asked by At

I'm trying to convert the following into a single line of a GeoDataFrame and failing

(geopandas version 0.12.2)

mpg = [{'type': 'Feature',
  'geometry': {'type': 'MultiPolygon',
   'coordinates': [[[385604.4471321234, -784020.4186040629],
     [414344.521612021, -784166.8937862087],
     [406426.80906232697, -773070.2847982664],
     [411716.97051107534, -772866.2662232779],
     [417817.61522901023, -778671.3963278867],
     [420320.6722005672, -793319.9315048195],
     [426264.54587735934, -800601.6001869421],
     [434725.6679308266, -801587.092414381],
     [436499.5447257533, -815616.8560287226],
     [445869.81354000897, -820017.0856200624],
     [423728.21757644176, -823920.3949416429],
     [417714.5664099267, -815857.826157527],
     [410812.3468634846, -814586.4896632992],
     [402610.73131904664, -823669.6734979339],
     [389789.8550130655, -820981.3267796431],
     [393773.755470313, -811766.5250452794],
     [386492.68404721894, -808969.8089811206],
     [389802.7575181274, -802826.8360282164],
     [384776.93156652513, -788369.1878023371],
     [385604.4471321234, -784020.4186040629]],
    [[436493.3613287912, -831894.4871440176],
     [457282.23320211814, -829161.0385349803],
     [464959.3743349153, -842692.3029331826],
     [473910.26534156053, -847251.3123871069],
     [473783.94691529335, -858540.9440790508],
     [462366.97489480156, -853369.9971767701],
     [445652.7230171143, -850591.3993632644],
     [446619.1470749643, -844911.6836319976],
     [436493.3613287912, -831894.4871440176]]]},
  'properties': {'OBJECTID': 641,
   'FIPSNum': 22099,
   'CountyFIPS': 22099,
   'CountyName': 'St Martin Parish',
   'MSAFIPS': 'C2918',
   'MSAName': 'Lafayette, LA MSA',
   'StateFips': 22,
   'StateAbbr': 'LA',
   'StateName': 'Louisiana',
   'County_SqMi': 817.98145282,
   'A_A1_1010_151': 0.92,
   'A_A1_1020_9': 1.1,
   'A_A1_1030_2': 0.81,
   'A_A1_1040_15': 51.3,
   'A_A1_1050_152': 0.22,
   'A_A1_1060_149': 1.59,
   'A_A1_1070_21': 0.09,
   'A_A1_1080_17': -0.01,
   'A_A2_1090_174': 45.0,
   'A_A2_1100_22': 0.56,
   'A_A2_1110_26': 0.48,
   'A_A2_1120_25': 14.0,
   'A_A2_1130_155': 0.78,
   'B_B1_2010_33': 2666.87,
   'B_B1_2020_36': 0.03,
   'B_B1_2030_37': 4.0,
   'B_B1_2040_29': 85.0,
   'B_B1_2050_27': 12803082.04,
   'B_B2_2060_34': 0.98,
   'B_B2_2070_41': 0.0,
   'B_B2_2080_43': 0.0,
   'B_B2_2090_42': 0.08,
   'B_B2_2100_44': 0.16,
   'B_B3_2110_82': -0.76,
   'B_B3_2120_81': 0.97,
   'B_B3_2130_91': 7.32,
   'B_B3_2140_215': 0.25,
   'C_C1_3010_50': 0.78,
   'C_C1_3020_55': 0.86,
   'C_C1_3030_166': 0.0,
   'C_C1_3040_47': 150121.67,
   'C_C1_3050_163': 0.72,
   'C_C1_3060_61': 0.01,
   'C_C2_3070_165': 0.0,
   'C_C2_3080_168': 0.0,
   'C_C2_3090_58': 1.0,
   'C_C3_3100_176': 130.63,
   'C_C3_3110_162': 0.11,
   'C_C3_3120_167': 151.0,
   'C_C3_3130_169': 1.0,
   'D_D1_4010_64': 50.0,
   'D_D1_4020_210': 2.05,
   'D_D1_4030_211': 2.06,
   'D_D1_4040_212': 2.06,
   'D_D1_4050_213': 2.07,
   'D_D1_4060_214': 2.06,
   'E_E1_5010_63': 100.0,
   'E_E1_5020_160': 0.52,
   'E_E2_5030_72': 0.11,
   'E_E2_5040_71': 110.76,
   'E_E2_5050_73': 181.38,
   'E_E2_5060_74': 1.69,
   'E_E3_5070_79': 0.09,
   'F_F1_6010_179': 0.0,
   'F_F1_6020_92': 80.0,
   'F_F1_6030_182': 32.0,
   'F_F1_6040_93': 37.5,
   'F_F1_6050_183': 50.0,
   'G_G1_7010_94': 7.0,
   'G_G1_7020_95': 7.0,
   'G_G1_7030_96': 8.0,
   'G_G1_7040_97': 4.0,
   'G_G1_7050_98': 3.0,
   'G_G1_7060_99': 10.0,
   'G_G1_7070_100': -0.2,
   'G_G2_7080_104': 0.0,
   'G_G2_7090_171': 0.09,
   'G_G2_7100_101': -1.07,
   'G_G2_7110_102': 100.0,
   'G_G3_7120_107': 0.01,
   'G_G3_7130_108': 18.07,
   'G_G3_7140_173': 0.01,
   'G_G3_7150_106': 49.51,
   'H_H1_8010_195': 1.0,
   'H_H1_8020_196': 2.0,
   'H_H1_8030_197': 1.0,
   'H_H1_8040_198': 3.0,
   'H_H1_8050_199': 3.0,
   'H_H1_8060_200': 2.0,
   'H_H1_8070_201': 1.0,
   'H_H1_8080_202': 2.0,
   'H_H1_8090_203': 1.0,
   'H_H1_8100_204': 1.0,
   'H_H1_8110_205': 0.0,
   'H_H1_8120_206': 1.0,
   'H_H1_8130_207': 2.0,
   'H_H2_8140_208': 2.0,
   'H_H2_8150_209': 5.0,
   'H_H2_8160_127': 61.0,
   'H_H3_8170_132': 0.05,
   'H_H4_8180_181': 0.0,
   'H_H4_8190_133': 0.13,
   'H_H4_8200_134': 0.07,
   'H_H5_8210_135': 105.0,
   'H_H5_8220_136': 99.93,
   'H_H5_8230_137': 6.0,
   'I_I1_9010_139': 352.06,
   'I_I1_9020_180': 53431.0,
   'J_J1_10010_141': 39604.96,
   'J_J2_10020_142': 7.33,
   'J_J2_10030_147': 0.05,
   'J_J3_10040_143': 46475.0,
   'J_J4_10050_146': 0.72,
   'J_J4_10060_191': 0.22,
   'J_J4_10070_144': 0.02,
   'J_J4_10080_145': 0.08,
   'Shape__Area': 2841742695.4100175,
   'Shape__Length': 384807.16591099254}}]
gdf_county2 = gpd.GeoDataFrame.from_features(mpg,  crs='ESRI:102003')

I get the following error:

  File ~\Anaconda3\envs\py39\lib\site-packages\geopandas\geodataframe.py:641, in GeoDataFrame.from_features(cls, features, crs, columns)
    638 if hasattr(feature, "__geo_interface__"):
    639     feature = feature.__geo_interface__
    640 row = {
--> 641     "geometry": shape(feature["geometry"]) if feature["geometry"] else None
    642 }
    643 # load properties
    644 properties = feature["properties"]

File ~\Anaconda3\envs\py39\lib\site-packages\shapely\geometry\geo.py:107, in shape(context)
    105     return MultiLineString(ob["coordinates"])
    106 elif geom_type == "multipolygon":
--> 107     return MultiPolygon([[c[0], c[1:]] for c in ob["coordinates"]])
    108 elif geom_type == "geometrycollection":
    109     geoms = [shape(g) for g in ob.get("geometries", [])]

File ~\Anaconda3\envs\py39\lib\site-packages\shapely\geometry\multipolygon.py:84, in MultiPolygon.__new__(self, polygons)
     82     else:
     83         holes = None
---> 84     p = polygon.Polygon(shell, holes)
     85 else:
     86     p = polygon.Polygon(ob)

File ~\Anaconda3\envs\py39\lib\site-packages\shapely\geometry\polygon.py:230, in Polygon.__new__(self, shell, holes)
    228     return shell
    229 else:
--> 230     shell = LinearRing(shell)
    232 if holes is not None:
    233     if len(holes) == 0:
    234         # shapely constructor cannot handle holes=[]

File ~\Anaconda3\envs\py39\lib\site-packages\shapely\geometry\polygon.py:93, in LinearRing.__new__(self, coordinates)
     90     else:
     91         return [float(c) for c in o]
---> 93 coordinates = np.array([_coords(o) for o in coordinates])
     94 if not np.issubdtype(coordinates.dtype, np.number):
     95     # conversion of coords to 2D array failed, this might be due
     96     # to inconsistent coordinate dimensionality
     97     raise ValueError("Inconsistent coordinate dimensionality")

File ~\Anaconda3\envs\py39\lib\site-packages\shapely\geometry\polygon.py:93, in <listcomp>(.0)
     90     else:
     91         return [float(c) for c in o]
---> 93 coordinates = np.array([_coords(o) for o in coordinates])
     94 if not np.issubdtype(coordinates.dtype, np.number):
     95     # conversion of coords to 2D array failed, this might be due
     96     # to inconsistent coordinate dimensionality
     97     raise ValueError("Inconsistent coordinate dimensionality")

File ~\Anaconda3\envs\py39\lib\site-packages\shapely\geometry\polygon.py:91, in LinearRing.__new__.<locals>._coords(o)
     89     return o.coords[0]
     90 else:
---> 91     return [float(c) for c in o]

TypeError: 'float' object is not iterable

However, the function works for polygons

pg = [{'type': 'Feature',
 'geometry': {'type': 'Polygon',
  'coordinates': [[[1542624.5639654002, -159268.0019311998],
    [1551448.7239454046, -155885.43987071142],
    [1576831.9281044968, -151763.00411699153],
    [1578531.3975074172, -150278.72921757773],
    [1589997.8605625234, -160280.42768909596],
    [1590057.0065687965, -161202.30578865856],
    [1592465.6797902645, -165364.5153435301],
    [1606229.5696370844, -179517.09021124803],
    [1607071.4817860047, -184477.21781846322],
    [1615041.356880301, -187891.35006070696],
    [1609281.782362619, -196480.08827992342],
    [1602905.931969609, -201315.15835905075],
    [1592769.0906640557, -202093.26320108958],
    [1572857.4845413638, -196299.03836596757],
    [1556508.209552073, -199107.30761205405],
    [1552601.1752610286, -197296.72046285495],
    [1554156.2291529258, -173772.7753984835],
    [1542624.5639654002, -159268.0019311998]]]},
 'properties': {'OBJECTID': 1,
  'FIPSNum': 37017,
  'CountyFIPS': 37017,
  'CountyName': 'Bladen County',
  'MSAFIPS': 'A2218',
  'MSAName': 'Fayetteville, NC MSA',
  'StateFips': 37,
  'StateAbbr': 'NC',
  'StateName': 'North Carolina',
  'County_SqMi': 879.83262325,
  'A_A1_1010_151': 0.99,
  'A_A1_1020_9': 1.19,
  'A_A1_1030_2': 0.98,
  'A_A1_1040_15': 63.3,
  'A_A1_1050_152': 0.65,
  'A_A1_1060_149': 1.65,
  'A_A1_1070_21': 0.09,
  'A_A1_1080_17': 0.01,
  'A_A2_1090_174': 38.43,
  'A_A2_1100_22': 0.69,
  'A_A2_1110_26': 0.82,
  'A_A2_1120_25': 39.0,
  'A_A2_1130_155': 0.94,
  'B_B1_2010_33': 3697.33,
  'B_B1_2020_36': 0.04,
  'B_B1_2030_37': 6.0,
  'B_B1_2040_29': 100.0,
  'B_B1_2050_27': 66995157.15,
  'B_B2_2060_34': 1.54,
  'B_B2_2070_41': 0.0,
  'B_B2_2080_43': 0.0,
  'B_B2_2090_42': 0.06,
  'B_B2_2100_44': 0.02,
  'B_B3_2110_82': 0.96,
  'B_B3_2120_81': 1.04,
  'B_B3_2130_91': 4.79,
  'B_B3_2140_215': 0.05,
  'C_C1_3010_50': 0.68,
  'C_C1_3020_55': 0.81,
  'C_C1_3030_166': 0.0,
  'C_C1_3040_47': 139623.09,
  'C_C1_3050_163': 1.17,
  'C_C1_3060_61': 0.0,
  'C_C2_3070_165': 37.5,
  'C_C2_3080_168': 3.0,
  'C_C2_3090_58': 1.0,
  'C_C3_3100_176': 55.49,
  'C_C3_3110_162': 0.09,
  'C_C3_3120_167': 151.0,
  'C_C3_3130_169': 1.0,
  'D_D1_4010_64': 100.0,
  'D_D1_4020_210': 3.41,
  'D_D1_4030_211': 3.42,
  'D_D1_4040_212': 3.42,
  'D_D1_4050_213': 3.42,
  'D_D1_4060_214': 3.42,
  'E_E1_5010_63': 100.0,
  'E_E1_5020_160': 0.11,
  'E_E2_5030_72': 0.23,
  'E_E2_5040_71': 114.33,
  'E_E2_5050_73': 108.61,
  'E_E2_5060_74': 1.27,
  'E_E3_5070_79': 0.24,
  'F_F1_6010_179': 90.0,
  'F_F1_6020_92': 60.8,
  'F_F1_6030_182': 48.0,
  'F_F1_6040_93': 75.0,
  'F_F1_6050_183': 50.0,
  'G_G1_7010_94': 5.0,
  'G_G1_7020_95': 6.0,
  'G_G1_7030_96': 8.0,
  'G_G1_7040_97': 1.0,
  'G_G1_7050_98': 3.0,
  'G_G1_7060_99': 5.0,
  'G_G1_7070_100': 0.5,
  'G_G2_7080_104': 0.0,
  'G_G2_7090_171': 0.11,
  'G_G2_7100_101': -0.33,
  'G_G2_7110_102': 75.0,
  'G_G3_7120_107': 0.01,
  'G_G3_7130_108': 23.14,
  'G_G3_7140_173': 0.34,
  'G_G3_7150_106': 60.45,
  'H_H1_8010_195': 2.0,
  'H_H1_8020_196': 1.0,
  'H_H1_8030_197': 1.0,
  'H_H1_8040_198': 4.0,
  'H_H1_8050_199': 3.0,
  'H_H1_8060_200': 3.0,
  'H_H1_8070_201': 1.0,
  'H_H1_8080_202': 2.0,
  'H_H1_8090_203': 1.0,
  'H_H1_8100_204': 1.0,
  'H_H1_8110_205': 0.0,
  'H_H1_8120_206': 1.0,
  'H_H1_8130_207': 3.0,
  'H_H2_8140_208': 4.0,
  'H_H2_8150_209': 2.0,
  'H_H2_8160_127': 70.0,
  'H_H3_8170_132': 0.04,
  'H_H4_8180_181': 0.0,
  'H_H4_8190_133': 0.07,
  'H_H4_8200_134': 0.02,
  'H_H5_8210_135': 104.0,
  'H_H5_8220_136': 99.4,
  'H_H5_8230_137': 10.0,
  'I_I1_9010_139': 187.52,
  'I_I1_9020_180': 18406.13,
  'J_J1_10010_141': 35907.81,
  'J_J2_10020_142': 8.08,
  'J_J2_10030_147': 0.0,
  'J_J3_10040_143': 58612.0,
  'J_J4_10050_146': 0.34,
  'J_J4_10060_191': 0.3,
  'J_J4_10070_144': 0.01,
  'J_J4_10080_145': 0.07,
  'Shape__Area': 3372387348.5245733,
  'Shape__Length': 248474.0412585642}}]

Why is it failing for multipolygons?

For Polygons, the following output is observed.

geometry OBJECTID FIPSNum CountyFIPS CountyName MSAFIPS MSAName StateFips StateAbbr StateName ... J_J1_10010_141 J_J2_10020_142 J_J2_10030_147 J_J3_10040_143 J_J4_10050_146 J_J4_10060_191 J_J4_10070_144 J_J4_10080_145 Shape__Area Shape__Length 0 POLYGON ((1542624.564 -159268.002, 1551448.724... 1 37017 37017 Bladen County A2218 Fayetteville, NC MSA 37 NC North Carolina ... 35907.81 8.08 0.0 58612.0 0.34 0.3 0.01 0.07 3.372387e+09 248474.041259

Pandas single row dataframe

1

There are 1 answers

0
Pieter On

The coordinates array for the multipolygon should be one level deeper, like this:

import geopandas as gpd

mpg = [{'type': 'Feature',
  'geometry': {'type': 'MultiPolygon',
   'coordinates': [[[[385604.4471321234, -784020.4186040629],
     [414344.521612021, -784166.8937862087],
     [406426.80906232697, -773070.2847982664],
     [411716.97051107534, -772866.2662232779],
     [417817.61522901023, -778671.3963278867],
     [420320.6722005672, -793319.9315048195],
     [426264.54587735934, -800601.6001869421],
     [434725.6679308266, -801587.092414381],
     [436499.5447257533, -815616.8560287226],
     [445869.81354000897, -820017.0856200624],
     [423728.21757644176, -823920.3949416429],
     [417714.5664099267, -815857.826157527],
     [410812.3468634846, -814586.4896632992],
     [402610.73131904664, -823669.6734979339],
     [389789.8550130655, -820981.3267796431],
     [393773.755470313, -811766.5250452794],
     [386492.68404721894, -808969.8089811206],
     [389802.7575181274, -802826.8360282164],
     [384776.93156652513, -788369.1878023371],
     [385604.4471321234, -784020.4186040629]],
    [[436493.3613287912, -831894.4871440176],
     [457282.23320211814, -829161.0385349803],
     [464959.3743349153, -842692.3029331826],
     [473910.26534156053, -847251.3123871069],
     [473783.94691529335, -858540.9440790508],
     [462366.97489480156, -853369.9971767701],
     [445652.7230171143, -850591.3993632644],
     [446619.1470749643, -844911.6836319976],
     [436493.3613287912, -831894.4871440176]]]]},
  'properties': {'OBJECTID': 641,
   'FIPSNum': 22099,
   'CountyFIPS': 22099,
   'CountyName': 'St Martin Parish',
   'MSAFIPS': 'C2918',
   'MSAName': 'Lafayette, LA MSA',
   'StateFips': 22,
   'StateAbbr': 'LA',
   'StateName': 'Louisiana',
   'County_SqMi': 817.98145282,
   'A_A1_1010_151': 0.92,
   'A_A1_1020_9': 1.1,
   'A_A1_1030_2': 0.81,
   'A_A1_1040_15': 51.3,
   'A_A1_1050_152': 0.22,
   'A_A1_1060_149': 1.59,
   'A_A1_1070_21': 0.09,
   'A_A1_1080_17': -0.01,
   'A_A2_1090_174': 45.0,
   'A_A2_1100_22': 0.56,
   'A_A2_1110_26': 0.48,
   'A_A2_1120_25': 14.0,
   'A_A2_1130_155': 0.78,
   'B_B1_2010_33': 2666.87,
   'B_B1_2020_36': 0.03,
   'B_B1_2030_37': 4.0,
   'B_B1_2040_29': 85.0,
   'B_B1_2050_27': 12803082.04,
   'B_B2_2060_34': 0.98,
   'B_B2_2070_41': 0.0,
   'B_B2_2080_43': 0.0,
   'B_B2_2090_42': 0.08,
   'B_B2_2100_44': 0.16,
   'B_B3_2110_82': -0.76,
   'B_B3_2120_81': 0.97,
   'B_B3_2130_91': 7.32,
   'B_B3_2140_215': 0.25,
   'C_C1_3010_50': 0.78,
   'C_C1_3020_55': 0.86,
   'C_C1_3030_166': 0.0,
   'C_C1_3040_47': 150121.67,
   'C_C1_3050_163': 0.72,
   'C_C1_3060_61': 0.01,
   'C_C2_3070_165': 0.0,
   'C_C2_3080_168': 0.0,
   'C_C2_3090_58': 1.0,
   'C_C3_3100_176': 130.63,
   'C_C3_3110_162': 0.11,
   'C_C3_3120_167': 151.0,
   'C_C3_3130_169': 1.0,
   'D_D1_4010_64': 50.0,
   'D_D1_4020_210': 2.05,
   'D_D1_4030_211': 2.06,
   'D_D1_4040_212': 2.06,
   'D_D1_4050_213': 2.07,
   'D_D1_4060_214': 2.06,
   'E_E1_5010_63': 100.0,
   'E_E1_5020_160': 0.52,
   'E_E2_5030_72': 0.11,
   'E_E2_5040_71': 110.76,
   'E_E2_5050_73': 181.38,
   'E_E2_5060_74': 1.69,
   'E_E3_5070_79': 0.09,
   'F_F1_6010_179': 0.0,
   'F_F1_6020_92': 80.0,
   'F_F1_6030_182': 32.0,
   'F_F1_6040_93': 37.5,
   'F_F1_6050_183': 50.0,
   'G_G1_7010_94': 7.0,
   'G_G1_7020_95': 7.0,
   'G_G1_7030_96': 8.0,
   'G_G1_7040_97': 4.0,
   'G_G1_7050_98': 3.0,
   'G_G1_7060_99': 10.0,
   'G_G1_7070_100': -0.2,
   'G_G2_7080_104': 0.0,
   'G_G2_7090_171': 0.09,
   'G_G2_7100_101': -1.07,
   'G_G2_7110_102': 100.0,
   'G_G3_7120_107': 0.01,
   'G_G3_7130_108': 18.07,
   'G_G3_7140_173': 0.01,
   'G_G3_7150_106': 49.51,
   'H_H1_8010_195': 1.0,
   'H_H1_8020_196': 2.0,
   'H_H1_8030_197': 1.0,
   'H_H1_8040_198': 3.0,
   'H_H1_8050_199': 3.0,
   'H_H1_8060_200': 2.0,
   'H_H1_8070_201': 1.0,
   'H_H1_8080_202': 2.0,
   'H_H1_8090_203': 1.0,
   'H_H1_8100_204': 1.0,
   'H_H1_8110_205': 0.0,
   'H_H1_8120_206': 1.0,
   'H_H1_8130_207': 2.0,
   'H_H2_8140_208': 2.0,
   'H_H2_8150_209': 5.0,
   'H_H2_8160_127': 61.0,
   'H_H3_8170_132': 0.05,
   'H_H4_8180_181': 0.0,
   'H_H4_8190_133': 0.13,
   'H_H4_8200_134': 0.07,
   'H_H5_8210_135': 105.0,
   'H_H5_8220_136': 99.93,
   'H_H5_8230_137': 6.0,
   'I_I1_9010_139': 352.06,
   'I_I1_9020_180': 53431.0,
   'J_J1_10010_141': 39604.96,
   'J_J2_10020_142': 7.33,
   'J_J2_10030_147': 0.05,
   'J_J3_10040_143': 46475.0,
   'J_J4_10050_146': 0.72,
   'J_J4_10060_191': 0.22,
   'J_J4_10070_144': 0.02,
   'J_J4_10080_145': 0.08,
   'Shape__Area': 2841742695.4100175,
   'Shape__Length': 384807.16591099254}}]
gdf_county2 = gpd.GeoDataFrame.from_features(mpg,  crs='ESRI:102003')
print(gdf_county2)