For SUMO, it is often necessary to convert from other files to SUMO . n e t . x m l .net.xml .net.xml file. In this article, I mainly record how I got from . s h p .shp .shp file into . n e t . x m l .net.xml .net.xml file.
SUMO's official documentation says that it comes with n e t c o n v e r t netconvert netconvert is convertible . s h p .shp .shp file. But that conversion is not supported in the official version of SUMO, and can only be used by downloading an unstable version. After I switched the system to use it under Windows, I found that it was still very difficult to use, and I had to add various parameters according to the map properties. So I gave up on this approach.
Finally, it was decided to use the . s h p .shp .shp converts to . o s m .osm .osm and then convert to n e t . x m l net.xml net.xml method.
This article is in reverse order, first transformed into . n e t . x m l .net.xml .net.xml, how to convert it to . o s m .osm .osm .
1. Convert .osm to .net.xml
Import the converted directly and find that the result is:
No nodes loaded.
It seems that no one in China has solved this problem, so I used some methods to go outside to see how to solve it, and then found the problem in a forum:
First, to explain . o s m .osm The format of the .osm file. This file is essentially an xml file. It mainly includes two parts: node and way, which represent nodes and roads respectively.
<osm version="0.6" upload="false" generator="JOSM"> <node id="-101752" lat="30.54631145" lon="114.32433762" /> <node id="-101753" lat="30.54612155" lon="114.32424762" /> <node id="-101754" lat="30.54557183" lon="114.32395746" /> <node id="-101755" lat="30.5454519" lon="114.32388744" /> <node id="-101756" lat="30.54521606" lon="114.34632894" /> ...... <way id="-111257"> <nd ref="-121187" /> <nd ref="-121188" /> <nd ref="-121189" /> <nd ref="-121190" /> <nd ref="-121191" /> <nd ref="-121192" /> <nd ref="-121193" /> <tag k="Name" v="武汉大道" /> <tag k="elevation" v="0" /> <tag k="gml_id" v="layer_provincial_highway_pl.518907"> <tag k="kind" v="0302|0308" /> <tag k="length" v="1.006" /> <tag k="pyname" v="whdd" /> <tag k="speedclass" v="5" /> <tag k="width" v="55" /> </way> <way id="-111259"> <nd ref="-121194" /> <nd ref="-121195" /> <tag k="Name" v="武汉大道" /> <tag k="elevation" v="0" /> <tag k="gml_id" v="layer_provincial_highway_pl.519507"> <tag k="kind" v="0302" /> <tag k="length" v="0.052" /> <tag k="pyname" v="whdd" /> <tag k="speedclass" v="5" /> <tag k="width" v="55" /> </way> ...... </osm>
is similar to this.
So why No nodes loaded? Because to be able to convert, the following two conditions must be met:
- There must be two kinds of tag objects, node and way. (this is satisfying)
- There must be a label with k="highway" in some way (not satisfied)
Article 2 means that there must be
<tag k="highway" v="xxxx(road type)"/>
Only then can. come see that official documentation Some parameter definitions for v(value):
And we found that in the above file, there is this:
<tag k="gml_id" v="layer_provincial_highway_pl.519507">
Does this store the road type?
not like this. In fact, I feel that every shp file is different. I compared the files of Open Street Map one by one, and finally found that the type of road is stored in k i n d kind kind inside. This is an encrypted call, what 0601, 0402, 0814, what the hell!
Then, I compared side by side, and after recording and summarizing, I found the corresponding rule:
kind | corresponding value |
---|---|
0601/080a | tertiary highway |
0602 | D |
0402/0412/080a | first class highway |
0801 | living_street |
0202/060a | Freeway |
0814 | Resident Road |
Then convert it, and find that some of the converted edges are actually broken! Quickly take a look at it and find that there is another key: k i n d _ n u m kind\_num kind_num. Some edges correspond to several types! (actually two at most)
There are two types, such as "0601|080a". Comparing the OpenStreetMap files, it is found that at least one of the two is listed in the table above, and it is the same as the one or two listed. The corresponding kind is the same! That is to say, in this case, both sides are taken out. The first one in the table is processed according to the kind of the first one, otherwise it is processed according to the second one.
Above code:
import xml.etree.ElementTree as ET tree = ET.parse("wow.osm") root = tree.getroot() ans = 0 def vis(namae) : if(namae == '0601' or namae == '0602' or namae == '0402' or namae == '080a') : return True if(namae == '0412' or namae == '0801' or namae == '0801' ) : return True if(namae == '0202' or namae == '060a' or namae == '0814') : return True return False for child in root : if (child.tag == 'way') : for child2 in child : if(child2.tag == 'tag' and child2.attrib['k'] == 'kind_num'): if(child2.attrib['v'] == '2') : for child3 in child : if (child3.tag == 'tag' and child3.attrib['k'] == 'kind'): c = child3.attrib['v'] if(vis(c[0:4]) == True) : child3.attrib['v'] = c[0:4] else: child3.attrib['v'] = c[5:9] for child2 in child: if(child2.tag == 'tag' and child2.attrib['k'] == 'kind') : c = child2.attrib['v'] if(c == '0402' or c == '0412' or c == '080a') : tage = ET.SubElement(child2, 'tag') tage.set('k', 'highway') tage.set('v', 'primary') if(c == '0602') : tage = ET.SubElement(child2, 'tag') tage.set('k', 'highway') tage.set('v', 'secondary') if(c == '0601') : tage = ET.SubElement(child2, 'tag') tage.set('k', 'highway') tage.set('v', 'tertiary') if(c == '0801') : tage = ET.SubElement(child2, 'tag') tage.set('k', 'highway') tage.set('v', 'living_street') if(c == '0814') : tage = ET.SubElement(child2, 'tag') tage.set('k', 'highway') tage.set('v', 'service') if(c == '0202' or c == '060a') : tage = ET.SubElement(child2, 'tag') tage.set('k', 'highway') tage.set('v', 'trunk') with open('wow.osm', 'wb') as f: tree.write(f)
Then netconvert can be success ful!
2 .shp converted to .osm
Before no nodes loaded, there must be some other error that will tell you that a property is invalid. At this time, you need to go to ArcMap to delete that attribute first, and then save it.
Transcoding is done afterwards. Because the original file probably does not support Chinese characters, it is prone to garbled characters. At this time, you can go to the Internet to find a GIS transcoder to use. can look at this QGIS transcoding.
2.1 JOSM transformation
Download first J O S M JOSM JOSM,JOSM official website. but since I am not W i n d o w s Windows Windows users, so don't know much about W i n d o w s Windows How to operate under Windows. m a c O S macOS macOS can directly take advantage of h o m e b r e w homebrew homebrew to install.
After the installation is complete, open it and find "Image" in the menu bar
Then click Image Preferences.
Click that plugin button, then search for
o
p
e
n
d
a
t
a
opendata
opendata, download and install. This will put
s
h
a
p
e
shape
Drag the shape file into it and save it as
o
s
m
osm
osm format.
2.2 shp2osm
this is a p e r l perl The perl command can be found on the Internet.
can look at this How to use shp2osm.