From 6b0805bfeb690ca4c5ce294dab6540d41f2fe268 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Fri, 11 Nov 2011 00:08:01 +0100 Subject: [PATCH] Add SRTM scripts --- config/contours/contours.sh | 31 +++++++++ config/contours/extract.py | 129 ++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100755 config/contours/contours.sh create mode 100755 config/contours/extract.py diff --git a/config/contours/contours.sh b/config/contours/contours.sh new file mode 100755 index 0000000000..fadedc9a53 --- /dev/null +++ b/config/contours/contours.sh @@ -0,0 +1,31 @@ +#where this script stands +WORKDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +cd ${WORKDIR} + +TMPDIR=${WORKDIR}/tmp/ +OSMDIR=${WORKDIR}/osm/ +INDEXDIR=${WORKDIR}/index/ +OUTDIR=${WORKDIR}/output/ +POLYDIR=${WORKDIR}/polygons/ + +for X in ${POLYDIR}*.poly; do + echo $X + base=$(basename $X) # get the basename without the path + extractname=`echo "${base%.*}"` #remove the extension + # Capitalize: + for i in $extractname; do B=`echo -n "${i:0:1}" | tr "[a-z]" "[A-Z]"`; cap=`echo -n "${B}${i:1}" `; done + + if [ -f ${OUTDIR}${cap}_SRTM_contours.obf ] # Check for already processed files + then + echo "Nothing to do for "${cap} + else + echo ${X}"---"${cap} + ${WORK_DIR}./extract.py ${X} + ${WORK_DIR}./osmconvert32 ${TMPDIR}out.osm --drop-brokenrefs -B=$X > ${OSMDIR}out.osm + ${WORK_DIR}./batch_indexing.sh + mv ${INDEXDIR}Out_1.obf ${OUTDIR}${cap}_SRTM_contours.obf + rm ${INDEXDIR}* + rm ${TMPDIR}* + rm ${OSMDIR}* + fi +done diff --git a/config/contours/extract.py b/config/contours/extract.py new file mode 100755 index 0000000000..a8c182d368 --- /dev/null +++ b/config/contours/extract.py @@ -0,0 +1,129 @@ +#!/usr/bin/env python +# Source is GPL + +import sys, re +import pdb +import os +from lxml import etree + +if (len(sys.argv) > 1): + filename= sys.argv[1] +# Extract the bounding box around the .poly: +f=open(filename,'r') +maxx = -360; +maxy = -360; +minx = 360; +miny = 360; +for l in f.readlines(): + coord=re.findall('[0-9.E+-]+',l) + # At least numbers, at least two + if re.findall('[0-9.]+',l): + if len(coord)==2: + if float(coord[0]) > maxx: maxx= float(coord[0]) + if float(coord[1]) > maxy: maxy= float(coord[1]) + if float(coord[0]) < minx: minx= float(coord[0]) + if float(coord[1]) < miny: miny= float(coord[1]) +#print minx, miny, maxx, maxy + +# It is better to split the area on 0.25x0.25 degrees tiles to allow low +# memory usage of lxml and small lines +box=0.25 + +Xs=[] +x=int(minx) +while x < maxx+box: + Xs.append(x) + x += box +Ys=[] +y=int(miny) +while y < maxy+box: + Ys.append(y) + y += box + +for x in Xs: + for y in Ys: + #print y, x, y+1, x+1 + os.system('Srtm2Osm/./Srtm2Osm.exe -step 20 -bounds1 '\ + + str(y) +' '+ str(x)+' '+ str(y+box)+' '+ str(x+box)+' -o tmp/'\ + + str(y) +'-'+ str(x)+'.osm') + +wayid=1000000001 +nodeid=2000000001 + +# re-open the files to ensure uniques id before merging +for x in Xs: + for y in Ys: + file='tmp/'+str(y) +'-'+ str(x)+'.osm' + osmDoc=etree.parse(file) + + outFile=open('tmp/out-'+str(y) +'-'+ str(x)+'.osm','w') + inRoot= osmDoc.getroot() + ways = osmDoc.findall('way') + nodes = osmDoc.findall('node') + #print "nodes:", len (nodes) + #print "ways:", len(ways) + wayList={} + nodeList={} + # create a dict of nodes by id for faster processing + for node in nodes: + nodeList[node.get('id')]=node + # create a dict of ways by id for faster processing + for way in ways: + wayList[way.get('id')]=way + + outRoot = etree.Element("osm",version="0.5", generator="Srtm2Osm + post-process by Yvecai") + for way in ways: + nds=way.findall('nd') + newWay = etree.Element("way",id=str(wayid)) + # create nodes in the output tree + for nd in nds: + ref=nd.get('ref') + try: + newNode = etree.Element("node",id=str(nodeid), lon=nodeList[ref].get('lon'), \ + lat=nodeList[ref].get('lat')) + except: + newNode = etree.Element("node",id=str(nodeid), lon="0",\ + lat=nodeList[ref].get('lat')) # Srtm2Osm bug on lon="0" + newNd= etree.Element("nd",ref=str(nodeid)) + nodeid+=1 + outRoot.append(newNode) + newWay.append(newNd) + + newTag= etree.Element("tag",k="contour", v="elevation") + newWay.append(newTag) + tags=way.findall('tag') + for tag in tags: + if tag.get('k')=='ele': + ele=tag.get('v') + #pdb.set_trace() + newTag= etree.Element("tag",k="ele", v=ele) + newWay.append(newTag) + #pdb.set_trace() + # Add a 'name' tag to render elevation on 100m lines + if int(ele) % 100 == 0: + newTag= etree.Element("tag",k="name", v=ele) + newWay.append(newTag) + outRoot.append(newWay) + wayid+=1 + + outFile.write("") + outFile.write(etree.tostring(outRoot, pretty_print=True)) + outFile.close() +#print wayid, nodeid + + +outFile=open('tmp/out.osm','w') +outFile.write('') +outFile.write('') +for x in Xs: + for y in Ys: + f=open('tmp/out-'+str(y) +'-'+ str(x)+'.osm','r') + lines=f.readlines() + outFile.writelines(lines[2:-1]) + f.close() +outFile.write('') + +outFile.close() + + +