Skip to content

Commit

Permalink
making osmGet url configurable and changing order of bbox arguments, …
Browse files Browse the repository at this point in the history
…removing obsolete API, refs #12
  • Loading branch information
behrisch committed Mar 27, 2019
1 parent b840ccf commit 21f3dd2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 65 deletions.
102 changes: 41 additions & 61 deletions tools/osmGet.py
Expand Up @@ -31,8 +31,8 @@
import sumolib # noqa


def readCompressed(conn, query, filename):
conn.request("POST", "/api/interpreter", """
def readCompressed(conn, urlpath, query, filename):
conn.request("POST", "/" + urlpath, """
<osm-script timeout="240" element-limit="1073741824">
<union>
%s
Expand All @@ -56,26 +56,21 @@ def readCompressed(conn, query, filename):

optParser = optparse.OptionParser()
optParser.add_option("-p", "--prefix", default="osm", help="for output file")
optParser.add_option(
"-b", "--bbox", help="bounding box to retrieve in geo coordinates south,west,north,east")
optParser.add_option("-o", "--oldapi", action="store_true",
default=False, help="use old API for retrieval")
optParser.add_option("-b", "--bbox", help="bounding box to retrieve in geo coordinates west,south,east,north")
optParser.add_option("-t", "--tiles", type="int",
default=1, help="number of tiles when using old api")
optParser.add_option(
"-d", "--output-dir", help="optional output directory (must already exist)")
default=1, help="number of tiles the output gets split into")
optParser.add_option("-d", "--output-dir", help="optional output directory (must already exist)")
optParser.add_option("-a", "--area", type="int", help="area id to retrieve")
optParser.add_option(
"-x", "--polygon", help="calculate bounding box from polygon data in file")
optParser.add_option("-x", "--polygon", help="calculate bounding box from polygon data in file")
optParser.add_option("-u", "--url", default="www.overpass-api.de/api/interpreter",
help="Download from the given OpenStreetMap server")
# alternatives: overpass.kumi.systems/api/interpreter, sumo.dlr.de/osm/api/interpreter


def get(args=None):
(options, args) = optParser.parse_args(args=args)
if not options.bbox and not options.area and not options.polygon:
optParser.error(
"At least one of 'bbox' and 'area' and 'polygon' has to be set.")
if options.oldapi and options.area:
optParser.error("Only the new API supports 'area'.")
optParser.error("At least one of 'bbox' and 'area' and 'polygon' has to be set.")
if options.polygon:
west = 1e400
south = 1e400
Expand All @@ -90,61 +85,46 @@ def get(args=None):
east = max(point[0], east)
north = max(point[1], north)
if options.bbox:
south, west, north, east = [float(v) for v in options.bbox.split(',')]
west, south, east, north = [float(v) for v in options.bbox.split(',')]
if south > north or west > east:
optParser.error("Invalid geocoordinates in bbox.")

if options.output_dir:
options.prefix = path.join(options.output_dir, options.prefix)

if options.oldapi:
num = options.tiles
b = west
conn = httplib.HTTPConnection("api.openstreetmap.org")
for i in range(num):
e = b + (east - west) / float(num)
req = "/api/0.6/map?bbox=%s,%s,%s,%s" % (b, south, e, north)
conn.request("GET", req)
r = conn.getresponse()
print(req, r.status, r.reason)
out = open(
path.join(os.getcwd(), "%s%s_%s.osm.xml" % (options.prefix, i, num)), "w")
out.write(r.read())
out.close()
b = e
conn.close()
if "http" in options.url:
url = urlparse.urlparse(options.url)
else:
host = 'www.overpass-api.de'
# host= 'overpass.osm.rambler.ru'
port = 443

if os.environ.get("https_proxy") is not None:
proxy_url = os.environ.get("https_proxy")
url = urlparse.urlparse(proxy_url)
url = urlparse.urlparse("https://" + options.url)
if os.environ.get("https_proxy") is not None:
proxy_url = urlparse.urlparse(os.environ.get("https_proxy"))
conn = httplib.HTTPSConnection(proxy_url.hostname, proxy_url.port)
conn.set_tunnel(url.hostname, 443, {})
else:
if url.scheme == "https":
conn = httplib.HTTPSConnection(url.hostname, url.port)
conn.set_tunnel(host, port, {})
else:
conn = httplib.HTTPConnection(host)

if options.area:
if options.area < 3600000000:
options.area += 3600000000
readCompressed(conn, '<area-query ref="%s"/>' %
options.area, options.prefix + "_city.osm.xml")
if options.bbox or options.polygon:
if options.tiles == 1:
readCompressed(conn, '<bbox-query n="%s" s="%s" w="%s" e="%s"/>' %
(north, south, west, east), options.prefix + "_bbox.osm.xml")
else:
num = options.tiles
b = west
for i in range(num):
e = b + (east - west) / float(num)
readCompressed(conn, '<bbox-query n="%s" s="%s" w="%s" e="%s"/>' % (
north, south, b, e), "%s%s_%s.osm.xml" % (options.prefix, i, num))
b = e

conn.close()
conn = httplib.HTTPConnection(url.hostname, url.port)

if options.area:
if options.area < 3600000000:
options.area += 3600000000
readCompressed(conn, url.path, '<area-query ref="%s"/>' %
options.area, options.prefix + "_city.osm.xml")
if options.bbox or options.polygon:
if options.tiles == 1:
readCompressed(conn, url.path, '<bbox-query n="%s" s="%s" w="%s" e="%s"/>' %
(north, south, west, east), options.prefix + "_bbox.osm.xml")
else:
num = options.tiles
b = west
for i in range(num):
e = b + (east - west) / float(num)
readCompressed(conn, url.path, '<bbox-query n="%s" s="%s" w="%s" e="%s"/>' % (
north, south, b, e), "%s%s_%s.osm.xml" % (options.prefix, i, num))
b = e

conn.close()


if __name__ == "__main__":
Expand Down
8 changes: 4 additions & 4 deletions tools/webWizard/script.js
Expand Up @@ -439,13 +439,13 @@ on("ready", function(){
var width = cor.right - cor.left;
var height = cor.bottom - cor.top;
data.coords = [
cor.top + height * canvasRect[3],
cor.left + width * canvasRect[0],
cor.top + height * canvasRect[1],
cor.left + width * canvasRect[2]
cor.top + height * canvasRect[3],
cor.left + width * canvasRect[2],
cor.top + height * canvasRect[1]
];
} else
data.coords = [cor.bottom, cor.left, cor.top, cor.right];
data.coords = [cor.left, cor.bottom, cor.right, cor.top];

vehicleClasses.forEach(function(vehicleClass){
var result = vehicleClass.toJSON();
Expand Down

0 comments on commit 21f3dd2

Please sign in to comment.