flow_data = gpd.read_file(file,rows=5) print(flow_data.shape)->(5, 6) x = flow_data.explode(column='multilinestring') print(x.shape)->(5, 6)
Why isn’t geopandas exploding this series??? Below I have posted the first 5 rows. Clearly multilinestring and that function isn’t throwing any errors. Any suggestions?
Any other idea’s of how to convert multilinestring to linestring would be appreciated.
MULTILINESTRING ((-114.069319428291 51.142490075662, -114.069351728401 51.142506426546, -114.069382913029 51.142521272328, -114.069417706205 51.142537147964, -114.069460378555 51.142556320785, -114.069523581167 51.142585093051, -114.069557389994 51.142600143088, -114.069587588254 51.142613749738, -114.069613888175 51.142625566177, -114.070525246189 51.143048460138, -114.071047195435 51.143261534475, -114.071641603393 51.143546491357, -114.071966248245 51.143714200369, -114.07203601153 51.143747139301, -114.072063129151 51.143760135016, -114.072086739849 51.143770445002, -114.072178092113 51.14381936031, -114.072243990406 51.143859914778, -114.072321443997 51.143912855102, -114.072390757328 51.143967508432, -114.072446490364 51.144017897386, -114.072493963635 51.144067652272, -114.072529997539 51.144107798078, -114.072569441662 51.144159051081, -114.072602781236 51.144211590673, -114.072634648833 51.144267740485, -114.072659102029 51.14431559841, -114.072683026052 51.144379553402, -114.072840302451 51.14533966674, -114.072893975391 51.145439079622, -114.072946985591 51.145517741348)) MULTILINESTRING ((-114.078812654692 51.045985445975, -114.078856358385 51.045057786564)) MULTILINESTRING ((-113.911968728449 51.03788555028, -113.911611038472 51.037873836366, -113.911253347251 51.037862121357, -113.910895657638 51.037850405254, -113.910537968207 51.037838688057, -113.910262581705 51.037838898553, -113.909987195202 51.037839108401, -113.909436420765 51.037839526148, -113.909273445701 51.037839649662, -113.909127635063 51.037839759326, -113.908551990876 51.037840192222, -113.907691460032 51.037840668212, -113.907471243112 51.037840595657, -113.907096670741 51.03784047111, -113.906722096947 51.037840345361, -113.906347524583 51.037840218413, -113.905972952222 51.037840090264, -113.905598379866 51.037839960915, -113.905223807512 51.037839830366, -113.904849235163 51.037839698616, -113.904474661391 51.037839565664, -113.904100089047 51.037839432412, -113.903725516709 51.03783929706, -113.903350944375 51.037839160508, -113.902976372044 51.037839022756, -113.902601799718 51.037838883802, -113.902038946391 51.037838145489, -113.901476093085 51.037837404465, -113.900913241225 51.03783666163, -113.900350387963 51.037835915185, -113.899976312735 51.037836031641, -113.899602236082 51.037836146898, -113.899228160854 51.037836260959, -113.898854085626 51.037836373823, -113.898480010398 51.03783648549, -113.898105933743 51.037836595958, -113.897731858517 51.037836704331, -113.897357783289 51.037836812405, -113.89670276671 51.037836976065, -113.896047750131 51.037837136054, -113.895392734982 51.037837291474, -113.894737718406 51.037837444121, -113.894665641682 51.037837412444, -113.894609041134 51.037837388387, -113.893907869362 51.037837080326, -113.893206696178 51.037836768956, -113.892505524437 51.037836452481, -113.891804351285 51.037836132698, -113.891411344723 51.037836359735, -113.89101833816 51.03783658545, -113.89062533017 51.037836809843, -113.890232323603 51.037837032916, -113.889839317035 51.037837254667, -113.889446310465 51.037837475096, -113.889053302468 51.037837694203, -113.888660295895 51.037837911989, -113.888278981661 51.037837979614, -113.887897667427 51.037838046894, -113.887516353195 51.03783811203, -113.887135038962 51.037838176822, -113.886753724732 51.03783823947, -113.886372410501 51.037838301773, -113.885991096273 51.037838361933, -113.885609782046 51.03783842085, -113.885228467818 51.037838479421, -113.884847153593 51.037838535849, -113.884465839367 51.037838591931, -113.884084525144 51.037838645871, -113.883703210921 51.037838699466, -113.8833218967 51.037838750917, -113.882950915632 51.037838799787, -113.882579933137 51.037838848377, -113.882208950645 51.037838894891, -113.881837969578 51.037838941127, -113.881466987088 51.037838985285, -113.881096006026 51.037839028267, -113.880725023537 51.037839070969, -113.880354042477 51.037839111596, -113.879983059991 51.037839151943, -113.879612078934 51.037839190215, -113.87924109645 51.037839228207, -113.87887011397 51.037839264122, -113.878499132915 51.03783929976, -113.878128150438 51.03783933332, -113.877710249443 51.037839695087, -113.877292347018 51.037840055358, -113.876874446015 51.037840414137, -113.876456545008 51.037840771421, -113.876038642572 51.03784112721, -113.875620741557 51.037841481505, -113.875202839113 51.037841834305, -113.874784938092 51.037842185612, -113.874367037066 51.037842535425, -113.873949134611 51.037842883742, -113.873563567405 51.037843310499, -113.873178000195 51.037843735984, -113.872792431556 51.037844159297, -113.872406864336 51.037844582238, -113.872021295687 51.037845003006, -113.871635728458 51.037845423403, -113.871250161224 51.037845842528, -113.870864592561 51.037846259481, -113.870188687984 51.037845012673, -113.869512784877 51.037843761958, -113.868836880387 51.037842507332, -113.868160975941 51.037841248798, -113.867473030522 51.037841379597, -113.866785083682 51.037841506345, -113.866097138269 51.037841629945, -113.865409191436 51.037841748594)) MULTILINESTRING ((-114.034347723568 51.04125442693, -114.031660228407 51.040210084567, -114.02897402929 51.039166142881, -114.027259063116 51.038499598374, -114.027153327708 51.038460709986, -114.027084722374 51.038434512714, -114.027003557015 51.038404050872, -114.026901133209 51.038363231393, -114.02681417019 51.038329722332, -114.026720442665 51.03829316723, -114.026635411953 51.038259658493, -114.026531055668 51.038219448633, -114.0264315325 51.038180456596, -114.026335874574 51.038145730135, -114.026257607901 51.038115877525, -114.02611448056 51.038060867192, -114.026006262185 51.03802004822, -114.025862291769 51.037965825266, -114.025727017555 51.037913428735, -114.02564875309 51.037883575715, -114.025572421007 51.037856769743, -114.025496091441 51.037831792234, -114.025434254782 51.037812907468, -114.025377249191 51.037797070941, -114.0253250785 51.037786109376, -114.025278941762 51.037782523467, -114.025247467083 51.03777901528)) MULTILINESTRING ((-114.233868988224 51.019954329928, -114.233660173738 51.020050139175, -114.233459784056 51.020145226417, -114.233270553642 51.020240290138, -114.23308574496 51.020327343821, -114.232863075563 51.02043047628, -114.232649338551 51.020534922815, -114.232486813134 51.020615263455, -114.232308704278 51.020703635758, -114.232110527739 51.020796049003, -114.231930158813 51.020879091574, -114.231709681595 51.020975548848, -114.231558233582 51.02103986171, -114.231372908537 51.021115231466, -114.231139011547 51.021206379689, -114.230978613843 51.021266709943, -114.230818202474 51.021324371834, -114.230664467078 51.021378019873, -114.230475072425 51.02144240712, -114.23025892156 51.02151218132, -114.2300137756 51.021586012648, -114.229728506861 51.02167059142, -114.229487764147 51.021732409826, -114.229311143007 51.021775920989, -114.229159564148 51.021814892382, -114.228952231446 51.021863309615, -114.228771637559 51.021902337348, -114.228562045725 51.021945422874, -114.228319008978 51.021995241994, -114.228113855763 51.022032983505, -114.22783509923 51.022081537255, -114.227594234334 51.022119348372, -114.227310974809 51.022159907835, -114.227083467757 51.022191023294, -114.226878239644 51.022214093252, -114.226628088754 51.022239991862, -114.226288983528 51.022271322145, -114.225918618063 51.022300044614, -114.22553256906 51.02231679392, -114.225193372074 51.022329450322, -114.224869766067 51.022335408017, -114.224579613166 51.022335965994, -114.224354174614 51.022333731757, -114.224046041586 51.022327696558, -114.223771467194 51.022318886771, -114.22345444203 51.022300822531, -114.223135165792 51.022278762174, -114.222876138528 51.022253917886, -114.222534493491 51.022221228576, -114.222257573114 51.022188415184, -114.221976164852 51.022150275125, -114.221721533167 51.022110750401, -114.221187648072 51.022028150882, -114.220647103226 51.021941153808, -114.220023929387 51.021843641664, -114.219516898901 51.021763245776, -114.219058998434 51.021688089238, -114.218153609908 51.021544767866, -114.217885567266 51.021499924383, -114.217655792425 51.021465317599, -114.215431646301 51.021099617377, -114.214927877748 51.021003574017, -114.214355753459 51.020875332028, -114.213809434991 51.020731580293, -114.213526818683 51.020649180676, -114.213288196794 51.020575966754, -114.212767627241 51.020404055689, -114.212463733385 51.020296394987, -114.210853114511 51.019734501192, -114.208013364369 51.018744848501, -114.205881493538 51.017990099998, -114.203715286002 51.017247441201, -114.199682587193 51.015819663221, -114.199602782237 51.01576040493, -114.199547399035 51.01571935685, -114.199505656811 51.015693714631, -114.199488411756 51.015682315353, -114.199445781776 51.015660674185, -114.199397729322 51.015640756607, -114.1993107086 51.015608905911, -114.196700157903 51.014688031057, -114.195528663338 51.01427827017))
Advertisement
Answer
Quick tip for your next question ;) – since you’ve provided full WKT objects, we actually can create a MRE directly from your question:
import geopandas as gpd, pandas as pd, numpy as np, shapely.geometry shapes = """ MULTILINESTRING ((-114.069319428291 51.142490075662, -114.069351728401 51.142506426546, -114.069382913029 51.142521272328, -114.069417706205 51.142537147964, -114.069460378555 51.142556320785, -114.069523581167 51.142585093051, -114.069557389994 51.142600143088, -114.069587588254 51.142613749738, -114.069613888175 51.142625566177, -114.070525246189 51.143048460138, -114.071047195435 51.143261534475, -114.071641603393 51.143546491357, -114.071966248245 51.143714200369, -114.07203601153 51.143747139301, -114.072063129151 51.143760135016, -114.072086739849 51.143770445002, -114.072178092113 51.14381936031, -114.072243990406 51.143859914778, -114.072321443997 51.143912855102, -114.072390757328 51.143967508432, -114.072446490364 51.144017897386, -114.072493963635 51.144067652272, -114.072529997539 51.144107798078, -114.072569441662 51.144159051081, -114.072602781236 51.144211590673, -114.072634648833 51.144267740485, -114.072659102029 51.14431559841, -114.072683026052 51.144379553402, -114.072840302451 51.14533966674, -114.072893975391 51.145439079622, -114.072946985591 51.145517741348)) MULTILINESTRING ((-114.078812654692 51.045985445975, -114.078856358385 51.045057786564)) MULTILINESTRING ((-113.911968728449 51.03788555028, -113.911611038472 51.037873836366, -113.911253347251 51.037862121357, -113.910895657638 51.037850405254, -113.910537968207 51.037838688057, -113.910262581705 51.037838898553, -113.909987195202 51.037839108401, -113.909436420765 51.037839526148, -113.909273445701 51.037839649662, -113.909127635063 51.037839759326, -113.908551990876 51.037840192222, -113.907691460032 51.037840668212, -113.907471243112 51.037840595657, -113.907096670741 51.03784047111, -113.906722096947 51.037840345361, -113.906347524583 51.037840218413, -113.905972952222 51.037840090264, -113.905598379866 51.037839960915, -113.905223807512 51.037839830366, -113.904849235163 51.037839698616, -113.904474661391 51.037839565664, -113.904100089047 51.037839432412, -113.903725516709 51.03783929706, -113.903350944375 51.037839160508, -113.902976372044 51.037839022756, -113.902601799718 51.037838883802, -113.902038946391 51.037838145489, -113.901476093085 51.037837404465, -113.900913241225 51.03783666163, -113.900350387963 51.037835915185, -113.899976312735 51.037836031641, -113.899602236082 51.037836146898, -113.899228160854 51.037836260959, -113.898854085626 51.037836373823, -113.898480010398 51.03783648549, -113.898105933743 51.037836595958, -113.897731858517 51.037836704331, -113.897357783289 51.037836812405, -113.89670276671 51.037836976065, -113.896047750131 51.037837136054, -113.895392734982 51.037837291474, -113.894737718406 51.037837444121, -113.894665641682 51.037837412444, -113.894609041134 51.037837388387, -113.893907869362 51.037837080326, -113.893206696178 51.037836768956, -113.892505524437 51.037836452481, -113.891804351285 51.037836132698, -113.891411344723 51.037836359735, -113.89101833816 51.03783658545, -113.89062533017 51.037836809843, -113.890232323603 51.037837032916, -113.889839317035 51.037837254667, -113.889446310465 51.037837475096, -113.889053302468 51.037837694203, -113.888660295895 51.037837911989, -113.888278981661 51.037837979614, -113.887897667427 51.037838046894, -113.887516353195 51.03783811203, -113.887135038962 51.037838176822, -113.886753724732 51.03783823947, -113.886372410501 51.037838301773, -113.885991096273 51.037838361933, -113.885609782046 51.03783842085, -113.885228467818 51.037838479421, -113.884847153593 51.037838535849, -113.884465839367 51.037838591931, -113.884084525144 51.037838645871, -113.883703210921 51.037838699466, -113.8833218967 51.037838750917, -113.882950915632 51.037838799787, -113.882579933137 51.037838848377, -113.882208950645 51.037838894891, -113.881837969578 51.037838941127, -113.881466987088 51.037838985285, -113.881096006026 51.037839028267, -113.880725023537 51.037839070969, -113.880354042477 51.037839111596, -113.879983059991 51.037839151943, -113.879612078934 51.037839190215, -113.87924109645 51.037839228207, -113.87887011397 51.037839264122, -113.878499132915 51.03783929976, -113.878128150438 51.03783933332, -113.877710249443 51.037839695087, -113.877292347018 51.037840055358, -113.876874446015 51.037840414137, -113.876456545008 51.037840771421, -113.876038642572 51.03784112721, -113.875620741557 51.037841481505, -113.875202839113 51.037841834305, -113.874784938092 51.037842185612, -113.874367037066 51.037842535425, -113.873949134611 51.037842883742, -113.873563567405 51.037843310499, -113.873178000195 51.037843735984, -113.872792431556 51.037844159297, -113.872406864336 51.037844582238, -113.872021295687 51.037845003006, -113.871635728458 51.037845423403, -113.871250161224 51.037845842528, -113.870864592561 51.037846259481, -113.870188687984 51.037845012673, -113.869512784877 51.037843761958, -113.868836880387 51.037842507332, -113.868160975941 51.037841248798, -113.867473030522 51.037841379597, -113.866785083682 51.037841506345, -113.866097138269 51.037841629945, -113.865409191436 51.037841748594)) MULTILINESTRING ((-114.034347723568 51.04125442693, -114.031660228407 51.040210084567, -114.02897402929 51.039166142881, -114.027259063116 51.038499598374, -114.027153327708 51.038460709986, -114.027084722374 51.038434512714, -114.027003557015 51.038404050872, -114.026901133209 51.038363231393, -114.02681417019 51.038329722332, -114.026720442665 51.03829316723, -114.026635411953 51.038259658493, -114.026531055668 51.038219448633, -114.0264315325 51.038180456596, -114.026335874574 51.038145730135, -114.026257607901 51.038115877525, -114.02611448056 51.038060867192, -114.026006262185 51.03802004822, -114.025862291769 51.037965825266, -114.025727017555 51.037913428735, -114.02564875309 51.037883575715, -114.025572421007 51.037856769743, -114.025496091441 51.037831792234, -114.025434254782 51.037812907468, -114.025377249191 51.037797070941, -114.0253250785 51.037786109376, -114.025278941762 51.037782523467, -114.025247467083 51.03777901528)) MULTILINESTRING ((-114.233868988224 51.019954329928, -114.233660173738 51.020050139175, -114.233459784056 51.020145226417, -114.233270553642 51.020240290138, -114.23308574496 51.020327343821, -114.232863075563 51.02043047628, -114.232649338551 51.020534922815, -114.232486813134 51.020615263455, -114.232308704278 51.020703635758, -114.232110527739 51.020796049003, -114.231930158813 51.020879091574, -114.231709681595 51.020975548848, -114.231558233582 51.02103986171, -114.231372908537 51.021115231466, -114.231139011547 51.021206379689, -114.230978613843 51.021266709943, -114.230818202474 51.021324371834, -114.230664467078 51.021378019873, -114.230475072425 51.02144240712, -114.23025892156 51.02151218132, -114.2300137756 51.021586012648, -114.229728506861 51.02167059142, -114.229487764147 51.021732409826, -114.229311143007 51.021775920989, -114.229159564148 51.021814892382, -114.228952231446 51.021863309615, -114.228771637559 51.021902337348, -114.228562045725 51.021945422874, -114.228319008978 51.021995241994, -114.228113855763 51.022032983505, -114.22783509923 51.022081537255, -114.227594234334 51.022119348372, -114.227310974809 51.022159907835, -114.227083467757 51.022191023294, -114.226878239644 51.022214093252, -114.226628088754 51.022239991862, -114.226288983528 51.022271322145, -114.225918618063 51.022300044614, -114.22553256906 51.02231679392, -114.225193372074 51.022329450322, -114.224869766067 51.022335408017, -114.224579613166 51.022335965994, -114.224354174614 51.022333731757, -114.224046041586 51.022327696558, -114.223771467194 51.022318886771, -114.22345444203 51.022300822531, -114.223135165792 51.022278762174, -114.222876138528 51.022253917886, -114.222534493491 51.022221228576, -114.222257573114 51.022188415184, -114.221976164852 51.022150275125, -114.221721533167 51.022110750401, -114.221187648072 51.022028150882, -114.220647103226 51.021941153808, -114.220023929387 51.021843641664, -114.219516898901 51.021763245776, -114.219058998434 51.021688089238, -114.218153609908 51.021544767866, -114.217885567266 51.021499924383, -114.217655792425 51.021465317599, -114.215431646301 51.021099617377, -114.214927877748 51.021003574017, -114.214355753459 51.020875332028, -114.213809434991 51.020731580293, -114.213526818683 51.020649180676, -114.213288196794 51.020575966754, -114.212767627241 51.020404055689, -114.212463733385 51.020296394987, -114.210853114511 51.019734501192, -114.208013364369 51.018744848501, -114.205881493538 51.017990099998, -114.203715286002 51.017247441201, -114.199682587193 51.015819663221, -114.199602782237 51.01576040493, -114.199547399035 51.01571935685, -114.199505656811 51.015693714631, -114.199488411756 51.015682315353, -114.199445781776 51.015660674185, -114.199397729322 51.015640756607, -114.1993107086 51.015608905911, -114.196700157903 51.014688031057, -114.195528663338 51.01427827017)) """.strip().split("n") gdf = gpd.GeoDataFrame({'multilinestring': shapes})
In comments, you’ve figured out you can convert from WKT to a GeometryArray using geopandas.GeoSeries.from_wkt
:
In [17]: gdf['multilinestring'] = gpd.GeoSeries.from_wkt(gdf.multilinestring)
However, if you assign the result to the existing geodataframe, you need to tell geopandas that the new values are the geometry column. You think of geopandas as pandas + special handling of one single column. Pandas is fully capable of working with GeometryArray type data. Geopandas only really kicks into gear when it knows that one column is the special geometry column to which it can apply geospatial operations. So to tell it that “multilinestring” is this special column (or even “geometry” if you’ve assigned it after creating the GeoDataFrame), you need to call gdf.set_geometry
:
In [20]: gdf = gdf.set_geometry('multilinestring')
Now that we’ve done this, geopandas knows that “multilinestring” is the special geometry column. This would have raised an error prior to the last step:
In [21]: gdf.geometry Out[21]: 0 MULTILINESTRING ((-114.06932 51.14249, -114.06... 1 MULTILINESTRING ((-114.07881 51.04599, -114.07... 2 MULTILINESTRING ((-113.91197 51.03789, -113.91... 3 MULTILINESTRING ((-114.03435 51.04125, -114.03... 4 MULTILINESTRING ((-114.23387 51.01995, -114.23... Name: multilinestring, dtype: geometry
Now that we’ve done this, all of geopandas’ spatial ops, including explode
, will work as expected:
In [23]: gdf.explode(index_parts=True) Out[23]: multilinestring 0 0 LINESTRING (-114.06932 51.14249, -114.06935 51... 1 0 LINESTRING (-114.07881 51.04599, -114.07886 51... 2 0 LINESTRING (-113.91197 51.03789, -113.91161 51... 3 0 LINESTRING (-114.03435 51.04125, -114.03166 51... 4 0 LINESTRING (-114.23387 51.01995, -114.23366 51...
See the Geopandas User Guide for a larger discussion of the structure of the GeoDataFrame
and the importance of the designated geometry column. Here’s an exerpt:
A
GeoDataFrame
is a tabular data structure that contains aGeoSeries
.The most important property of a
GeoDataFrame
is that it always has oneGeoSeries
column that holds a special status. ThisGeoSeries
is referred to as theGeoDataFrame
’s “geometry”. When a spatial method is applied to aGeoDataFrame
(or a spatial attribute likearea
is called), this commands will always act on the “geometry” column.The “geometry” column – no matter its name – can be accessed through the
geometry
attribute (gdf.geometry
), and the name of thegeometry
column can be found by typinggdf.geometry.name
.A
GeoDataFrame
may also contain other columns with geometrical (shapely) objects, but only one column can be the active geometry at a time. To change which column is the active geometry column, use theGeoDataFrame.set_geometry()
method.