Fix osm generation if contour shapefile is empty, now return empty osm file.
This commit is contained in:
parent
c615b8adce
commit
82c7569da4
1 changed files with 56 additions and 54 deletions
|
@ -2,7 +2,6 @@
|
|||
import shapefile
|
||||
import os, sys
|
||||
from lxml import etree
|
||||
|
||||
from optparse import OptionParser
|
||||
|
||||
def listsplit(tosplit, n=50):
|
||||
|
@ -36,63 +35,66 @@ if not options.inputfilename:
|
|||
exit(1)
|
||||
|
||||
out=open(options.outputfilename,'w')
|
||||
sf = shapefile.Reader(options.inputfilename)
|
||||
|
||||
shapeRecs = sf.shapeRecords()
|
||||
print "shp loaded"
|
||||
|
||||
out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
|
||||
out.write("<osm generator=\"srtmshp2osm\" version=\"0.6\">")
|
||||
|
||||
index= 3000000000
|
||||
l=len(shapeRecs)
|
||||
for shapeRec in shapeRecs:
|
||||
height=int(shapeRec.record[1]) # there only one field in my shapefile, 'HEIGHT'
|
||||
pointList=shapeRec.shape.points # [[x1,y1],[x2,y2]...]
|
||||
splitPointList=listsplit(pointList,100)
|
||||
|
||||
for points in splitPointList:
|
||||
|
||||
# nodes:
|
||||
firstindex = index
|
||||
for p in points:
|
||||
node = etree.Element("node",
|
||||
id=str(index),
|
||||
lat=str(p[1]),
|
||||
lon=str(p[0]),
|
||||
version="1")
|
||||
out.write(etree.tostring(node, pretty_print=options.pretty))
|
||||
index +=1
|
||||
lastindex=index-1
|
||||
sf = shapefile.Reader(options.inputfilename)
|
||||
|
||||
if len(sf.shapes()) != 0:
|
||||
# read shapeRecords only if non-empty
|
||||
shapeRecs = sf.shapeRecords()
|
||||
print "shp loaded"
|
||||
|
||||
index= 3000000000
|
||||
l=len(shapeRecs)
|
||||
for shapeRec in shapeRecs:
|
||||
height=int(shapeRec.record[1]) # there only one field in my shapefile, 'HEIGHT'
|
||||
pointList=shapeRec.shape.points # [[x1,y1],[x2,y2]...]
|
||||
splitPointList=listsplit(pointList,100)
|
||||
|
||||
# way:
|
||||
way = etree.Element("way", id=str(index),version="1")
|
||||
index +=1
|
||||
for i in range(firstindex,lastindex):
|
||||
way.append(etree.Element("nd", ref=str(i)))
|
||||
|
||||
# tags:
|
||||
way.append(etree.Element("tag",k="elevation", v=str(height)))
|
||||
way.append(etree.Element("tag",k="contour", v="elevation"))
|
||||
if height % 100 ==0:
|
||||
way.append(etree.Element("tag",k="contourtype", v="100m"))
|
||||
way.append(etree.Element("tag",k="name", v=str(height)))
|
||||
elif height % 50 ==0:
|
||||
way.append(etree.Element("tag",k="contourtype", v="50m"))
|
||||
elif height % 20 ==0:
|
||||
way.append(etree.Element("tag",k="contourtype", v="20m"))
|
||||
elif height % 10 ==0:
|
||||
way.append(etree.Element("tag",k="contourtype", v="10m"))
|
||||
|
||||
out.write(etree.tostring(way, pretty_print=options.pretty))
|
||||
|
||||
#progress:
|
||||
l-=1
|
||||
if not options.quiet:
|
||||
sys.stdout.write("\r")
|
||||
sys.stdout.write("% 12d" % l)
|
||||
sys.stdout.flush()
|
||||
|
||||
for points in splitPointList:
|
||||
|
||||
# nodes:
|
||||
firstindex = index
|
||||
for p in points:
|
||||
node = etree.Element("node",
|
||||
id=str(index),
|
||||
lat=str(p[1]),
|
||||
lon=str(p[0]),
|
||||
version="1")
|
||||
out.write(etree.tostring(node, pretty_print=options.pretty))
|
||||
index +=1
|
||||
lastindex=index-1
|
||||
|
||||
# way:
|
||||
way = etree.Element("way", id=str(index),version="1")
|
||||
index +=1
|
||||
for i in range(firstindex,lastindex):
|
||||
way.append(etree.Element("nd", ref=str(i)))
|
||||
|
||||
# tags:
|
||||
way.append(etree.Element("tag",k="elevation", v=str(height)))
|
||||
way.append(etree.Element("tag",k="contour", v="elevation"))
|
||||
if height % 100 ==0:
|
||||
way.append(etree.Element("tag",k="contourtype", v="100m"))
|
||||
way.append(etree.Element("tag",k="name", v=str(height)))
|
||||
elif height % 50 ==0:
|
||||
way.append(etree.Element("tag",k="contourtype", v="50m"))
|
||||
elif height % 20 ==0:
|
||||
way.append(etree.Element("tag",k="contourtype", v="20m"))
|
||||
elif height % 10 ==0:
|
||||
way.append(etree.Element("tag",k="contourtype", v="10m"))
|
||||
|
||||
out.write(etree.tostring(way, pretty_print=options.pretty))
|
||||
|
||||
#progress:
|
||||
l-=1
|
||||
if not options.quiet:
|
||||
sys.stdout.write("\r")
|
||||
sys.stdout.write("% 12d" % l)
|
||||
sys.stdout.flush()
|
||||
else:
|
||||
print "Shapefile was empty"
|
||||
|
||||
print ""
|
||||
out.write("</osm>")
|
||||
|
|
Loading…
Reference in a new issue