cyang-kth / fmm

Fast map matching, an open source framework in C++
https://fmm-wiki.github.io/
Apache License 2.0
884 stars 211 forks source link

ValueError: Invalid field type <class 'tuple'> when try to save graph_shapefile_directional #166

Closed Mohammed-9089 closed 3 years ago

Mohammed-9089 commented 3 years ago

Let me say thanks for your very great and helpful work (Fast map matching algorithm), I am try to using it in Ubuntu operating system. First off all, when I go to implement the example of ("Stockholm, Sweden") in (../osmnx_example/download_network.ipynb) here, I am receiving an error as following:

bounds = (18.029122582902115, 18.070836297501724, 59.33476653724975, 59.352622230576124) x1,x2,y1,y2 = bounds boundary_polygon = Polygon([(x1,y1),(x2,y1),(x2,y2),(x1,y2)]) G = ox.graph_from_polygon(boundary_polygon, network_type='drive') start_time = time.time() save_graph_shapefile_directional(G, filepath='./stockholm') print("--- %s seconds ---" % (time.time() - start_time)) The error:

`--------------------------------------------------------------------------- ValueError Traceback (most recent call last)

in 4 G = ox.graph_from_polygon(boundary_polygon, network_type='drive') 5 start_time = time.time() ----> 6 save_graph_shapefile_directional(G, filepath='./stockholm') 7 print("--- %s seconds ---" % (time.time() - start_time)) in save_graph_shapefile_directional(G, filepath, encoding) 24 # save the nodes and edges as separate ESRI shapefiles 25 gdf_nodes.to_file(filepath_nodes, encoding=encoding) ---> 26 gdf_edges.to_file(filepath_edges, encoding=encoding) 27 28 print("osmnx version",ox.__version__) ~/anaconda3/lib/python3.8/site-packages/geopandas/geodataframe.py in to_file(self, filename, driver, schema, index, **kwargs) 744 from geopandas.io.file import _to_file 745 --> 746 _to_file(self, filename, driver, schema, index, **kwargs) 747 748 def set_crs(self, crs=None, epsg=None, inplace=False, allow_override=False): ~/anaconda3/lib/python3.8/site-packages/geopandas/io/file.py in _to_file(df, filename, driver, schema, index, mode, crs, **kwargs) 255 filename, mode=mode, driver=driver, crs_wkt=crs_wkt, schema=schema, **kwargs 256 ) as colxn: --> 257 colxn.writerecords(df.iterfeatures()) 258 259 ~/anaconda3/lib/python3.8/site-packages/fiona/collection.py in writerecords(self, records) 359 if self.mode not in ('a', 'w'): 360 raise IOError("collection not open for writing") --> 361 self.session.writerecs(records, self) 362 self._len = self.session.get_length() 363 self._bounds = None fiona/ogrext.pyx in fiona.ogrext.WritingSession.writerecs() fiona/ogrext.pyx in fiona.ogrext.OGRFeatureBuilder.build() ValueError: Invalid field type ` Does anyone have any suggestions? Thanks! @cyang-kth
cyang-kth commented 3 years ago

You need to check the details of the gdf_edges data frame to see which field causes this problem.

https://github.com/cyang-kth/osm_mapmatching/issues/13

Mohammed-9089 commented 3 years ago

Thanks for replying, yes I did it the "fid" column is contain tuples, so when I remove it I can save the file. So how can I address this issue, thanks. @cyang-kth

Mohammed-9089 commented 3 years ago

@cyang-kth The problem because "fid" column is contain tuples, so how can we remove it, while the fmm using this filed. (fmm/example/osmnx_example/map_matching.ipynb) here using this filed.

Read network and graph: network = Network("stockholm/edges.shp","fid", "u", "v")

cyang-kth commented 3 years ago

The fid field should not be tuple

gdf_edges["fid"] = gdf_edges.index

Can you check which row has this field of tuple?

Mohammed-9089 commented 3 years ago

All the data within the "fid" field like (119010, 208314436, 0), this is the first row,,, @cyang-kth

cyang-kth commented 3 years ago

All the fid should be integer. Can you provide more details about how this field looks like?

Mohammed-9089 commented 3 years ago

I'm still follow your example I didn't change anything (example of "Stockholm, Sweden") @cyang-kth

Mohammed-9089 commented 3 years ago

@cyang-kth All the fid should be integer. Can you provide more details about how this field looks like?,,,, it's not clear for me, did you mean some rows may contain null values or float?

cyang-kth commented 3 years ago

Try to replace the above line with

import numpy as np
gdf_edges["fid"] = np.arange(gdf_edges.shape[0])

See if you can save it successfully.

Mohammed-9089 commented 3 years ago

@cyang-kth

saved successfully, but when i moved to fmm notebook i faced other error network = Network("stockholm/edges.shp","fid", "u", "v") print "Nodes {} edges {}".format(network.get_node_count(),network.get_edge_count()) graph = NetworkGraph(network) the error is : RuntimeErrorTraceback (most recent call last)

in () ----> 1 network = Network("stockholm/edges.shp","fid", "u", "v") 2 print "Nodes {} edges {}".format(network.get_node_count(),network.get_edge_count()) 3 graph = NetworkGraph(network) /home/mohfatih89/anaconda3/envs/py36/lib/python2.7/fmm.pyc in __init__(self, *args) 898 899 def __init__(self, *args): --> 900 _fmm.Network_swiginit(self, _fmm.new_Network(*args)) 901 902 def get_node_count(self): RuntimeError: Id, source or target column not found
cyang-kth commented 3 years ago

Check the fields of the shapefile to see if you have fid and u, v fields.

Mohammed-9089 commented 3 years ago

@cyang-kth import geopandas as gp shp = gp.GeoDataFrame.from_file("stockholm/edges.shp") print (shp)

the output: u v key osmid \ 0 119010 208314436 0 8019936
1 119010 119011 0 41506578
2 119011 5172054123 0 [28274898, 25666151, 28274837, 299998023]
3 119011 21760826 0 299988378
4 119013 21760821 0 [276290333, 276290332, 19874181]
... ... ... ... ...
94552 8309055931 264226559 0 48452302
94553 8309055931 1473546234 0 893985416
94554 8309055955 111722354 0 37480274
94555 8309055955 629493511 0 893985447
94556 8309055972 2323935087 0 23138796

   oneway                                               name  \

0 1 Rålambsvägen
1 1 Västerbroplan
2 1 ['Långholmsgatan', 'Västerbroplan', 'Västerbron']
3 1 Västerbroplan
4 1 Västerbronedfarten
... ... ...
94552 0 Mjölnergatan
94553 1 Mjölnergatan
94554 0 Östra Galaxvägen
94555 1 Östra Galaxvägen
94556 0 Voltgatan

        highway      maxspeed    length lanes bridge   ref junction  \

0 tertiary 40 149.451 None None None None
1 secondary 40 45.518 3 None None None
2 secondary ['40', '60'] 1284.467 3 yes None None
3 secondary 40 68.854 None None None None
4 secondary 40 363.530 None None None None
... ... ... ... ... ... ... ...
94552 unclassified None 15.907 None None None None
94553 unclassified None 38.737 None None None None
94554 residential 50 55.572 None None None None
94555 residential 50 43.040 None None None None
94556 unclassified 50 200.667 None None None None

  tunnel access width service    fid  \

0 None None None None 0
1 None None None None 1
2 None None None None 2
3 None None None None 3
4 None None None None 4
... ... ... ... ... ...
94552 None None None None 94552
94553 None None None None 94553
94554 None None None None 94554
94555 None None None None 94555
94556 None None None None 94556

                                            geometry  

0 LINESTRING (18.02141 59.32853, 18.02127 59.328...
1 LINESTRING (18.02141 59.32853, 18.02132 59.328...
2 LINESTRING (18.02130 59.32815, 18.02139 59.328...
3 LINESTRING (18.02130 59.32815, 18.02153 59.328...
4 LINESTRING (18.02387 59.32727, 18.02378 59.327...
... ...
94552 LINESTRING (17.82677 59.61538, 17.82701 59.61545)
94553 LINESTRING (17.82677 59.61538, 17.82649 59.615...
94554 LINESTRING (17.82061 59.61569, 17.82046 59.61520)
94555 LINESTRING (17.82061 59.61569, 17.82079 59.616...
94556 LINESTRING (17.86802 59.61850, 17.86759 59.61671)

[94557 rows x 19 columns]

cyang-kth commented 3 years ago

Please pull the latest master version to see which field is not found in the new error message.

Mohammed-9089 commented 3 years ago

@cyang-kth Really helpful, thanks a lot, it works fine and now everything going well....!!!