Skip to content

Commit

Permalink
Use Python downloader (#61)
Browse files Browse the repository at this point in the history
* use python downloader

* update version to 1.0.0b4
  • Loading branch information
jiawlu authored Sep 26, 2024
1 parent 18fced0 commit a6f36eb
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 3 deletions.
3 changes: 1 addition & 2 deletions osm2gmns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

from osm2gmns.osm2gmns import initlib
from osm2gmns.osm2gmns import getNetFromFile, generateNodeActivityInfo, fillLinkAttributesWithDefaultValues, consolidateComplexIntersections, outputNetToCSV
from osm2gmns.downloader import downloadOSMData

__version__ = '1.0.0b3'
print(__version__)

initlib()

Expand Down
106 changes: 106 additions & 0 deletions osm2gmns/downloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env python
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
# Copyright (C) 2009-2020 German Aerospace Center (DLR) and others.
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0/
# This Source Code may also be made available under the following Secondary
# Licenses when the conditions for such availability set forth in the Eclipse
# Public License 2.0 are satisfied: GNU General Public License, version 2
# or later which is available at
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later

# @file osmGet.py
# @author Daniel Krajzewicz
# @author Jakob Erdmann
# @author Michael Behrisch
# @date 2009-08-01


import os
import http.client as httplib
import urllib.parse as urlparse
import base64



_url = "www.overpass-api.de/api/interpreter"
# alternatives: overpass.kumi.systems/api/interpreter, sumo.dlr.de/osm/api/interpreter


def _readCompressed(conn, urlpath, query, filename):
conn.request("POST", "/" + urlpath, """
<osm-script timeout="240" element-limit="1073741824">
<union>
%s
<recurse type="node-relation" into="rels"/>
<recurse type="node-way"/>
<recurse type="way-relation"/>
</union>
<union>
<item/>
<recurse type="way-node"/>
</union>
<print mode="body"/>
</osm-script>""" % query)
response = conn.getresponse()
# print(response.status, response.reason)
if response.status == 200:
print('valid reponses got from API server.')
print('receving data...')
out = open(filename, "wb")
out.write(response.read())
out.close()
print(f'map data has been written to {filename}')


def downloadOSMData(area_id, output_filename='map.osm', url=_url):
"""
Download OpenStreetMap data via overpass API
Parameters
----------
area_id: int
relation_id of the area of interest
output_filename: int
full path where the downloaded network will be stored
url: int
OpenStreetMap API url
Returns
-------
None
"""

file_name, file_extension = os.path.splitext(output_filename)
if not file_extension:
print(f'WARNING: no file extension in output_filename {output_filename}, output_filename is changed to {file_name}.osm')
output_filename = f'{file_name}.osm'
elif file_extension not in ['.osm', '.xml']:
print(f'WARNING: the file extension in output_filename {output_filename} is not supported, output_filename is changed to {file_name}.osm')
output_filename = f'{file_name}.osm'

if "http" in url:
url = urlparse.urlparse(url)
else:
url = urlparse.urlparse("https://" + url)
if os.environ.get("https_proxy") is not None:
headers = {}
proxy_url = urlparse.urlparse(os.environ.get("https_proxy"))
if proxy_url.username and proxy_url.password:
auth = '%s:%s' % (proxy_url.username, proxy_url.password)
headers['Proxy-Authorization'] = 'Basic ' + base64.b64encode(auth)
conn = httplib.HTTPSConnection(proxy_url.hostname, proxy_url.port)
conn.set_tunnel(url.hostname, 443, headers)
else:
if url.scheme == "https":
conn = httplib.HTTPSConnection(url.hostname, url.port)
else:
conn = httplib.HTTPConnection(url.hostname, url.port)

if area_id < 3600000000:
area_id += 3600000000
_readCompressed(conn, url.path, '<area-query ref="%s"/>' % area_id, output_filename)

conn.close()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build-backend = "scikit_build_core.build"

[project]
name = "osm2gmns"
version = "1.0.0b3"
version = "1.0.0b4"
description = "convert map data from OpenStreetMap to network files in GMNS format"
authors = [{ name = "Jiawei Lu", email = "[email protected]" }, { name = "Xuesong (Simon) Zhou", email = "[email protected]" }]
readme = "README.rst"
Expand Down

0 comments on commit a6f36eb

Please sign in to comment.