def main(input_filepath: str,
         output_filepath: str,
         overpass_server: str = DEFAULT_OVERPASS_SERVER) -> None:
    print('Opening query:', input_filepath)
    with open(input_filepath, 'r', encoding='utf-8') as f:
        query = f.read()

    response = None
    try:
        response = requests.post(overpass_server, data={'data': query})
    except:
        logging.error('Error for query:', input_filepath)

    if response:
        if response.ok:
            data = response.json()
            geojson = osm2geojson.json2geojson(data,
                                               filter_used_refs=False,
                                               log_level='INFO')
            print('Saving file:', output_filepath)
            with open(output_filepath, 'w', encoding='utf-8') as o:
                json.dump(geojson, o)
        else:
            print('Request was not successful.')
            print(response.status_code)
            print(response.text)
Esempio n. 2
0
 def _get_places_overpy(self, query):
     try:
         result = requests.get("http://overpass-api.de/api/interpreter",
                               data={
                                   "data": query
                               }).json()
         return osm2geojson.json2geojson(result)
     except:
         return {"features": [], "type": "FeatureCollection"}
Esempio n. 3
0
def convertGeoJson(in_path, out_path):
    with codecs.open(in_path, 'r', encoding='utf-8') as data:
        js = data.read()

    geojson = osm2geojson.json2geojson(js)
    s = json.dumps(geojson)
    s = s.encode()
    with open(out_path, 'wb') as f:
        f.write(s)
        f.close()
        return {"status": True, "message": "GeoJson Convert Successful"}
def get_border(id):
    file = os.path.join(GEOMETRY_DIR, f"border_{id}.geojson")
    if os.path.exists(file):
        with open(file, encoding='utf-8') as d:
            return json.load(d)['features'][0]

    data = cached_overpass_call(f"""
        [out:json];
        rel({id});
        out geom;
    """)
    geojson_data = json2geojson(data)

    border = None
    for f in geojson_data['features']:
        if f['properties']['tags']['type'] == 'boundary':
            border = f

    if border is not None:
        geojson_data['features'] = [border]
        save_data(geojson_data, file)
    return border
Esempio n. 5
0
    def getGeojsonFromOverpass(self):
        tagValue = self.filters["objectTags"]
        tagValues = [
            [i.strip() for i in t.split("=")]  # remove leading and ending spaces
            for t in tagValue.split(",")
            if len(t.split("=")) == 2
        ]

        areaQuery = ""
        overPassTagQuery = ""

        areas = self.filters["filterArea"]
        splittedAreas = areas.split(",")
        for area in splittedAreas:
            area = area.strip()
            searchArea = area.replace("ü", "u").replace("ä", "a").replace("ö", "o").replace(" ", "_")
            areaQuery += 'area[name="{areaName}"]->.search{searchArea};\n'.format(
                areaName=area, searchArea=searchArea
            )
            for tag in tagValues:
                overPassTagQuery += (
                    'nwr["{key}"="{value}"](area.search{searchArea});\n'.format(
                        key=tag[0], value=tag[1], areaName=area, searchArea=searchArea
                    )
                )

        overpass_url = settings.OVERPASS_URL
        overpass_query = (
            "[out:json];\n{0}(\n{1}\n);\nout body;\n>;\nout skel qt;".format(
                areaQuery, overPassTagQuery
            )
        )
        response = requests.get(overpass_url, params={"data": overpass_query})
        if response.status_code != 200 or not response.json()["elements"]:
            return None
        newObjectTagArea = response.json()
        return json2geojson(newObjectTagArea)
def get_outdoor_seating_nodes(id):
    file = os.path.join(GEOMETRY_DIR, f"seatings_{id}.geojson")
    if os.path.exists(file):
        with open(file, encoding='utf-8') as f:
            return json.load(f)

    border = get_border(id)
    border_shape = geometry.shape(border['geometry'])
    minlon, minlat, maxlon, maxlat = border_shape.bounds
    bbox = f"({minlat}, {minlon}, {maxlat}, {maxlon})"
    amenities = ['restaurant', 'pub', 'bar', 'cafe', 'fast_food', 'bbq', 'biergarten', 'food_court']

    data = cached_overpass_call(f"""
        [out:json];
        node[outdoor_seating=yes][amenity~"^{'$|^'.join(amenities)}$"]{bbox};
        out geom;
    """)

    geojson_data = json2geojson(data, filter_used_refs=False)
    geojson_data = get_features_inside_shape(geojson_data, border_shape)
    for feature in geojson_data['features']:
        feature['properties']['name'] = feature['properties']['tags'].get('name', 'noname')
    save_data(geojson_data, file)
    return geojson_data
Esempio n. 7
0
def import_outline(body: Body, gemeindeschluessel: Optional[str] = None):
    gemeindeschluessel = gemeindeschluessel or body.ags
    assert gemeindeschluessel is not None

    logger.info("Importing outline from {}".format(gemeindeschluessel))

    if not body.outline:
        outline = Location()
        outline.name = "Outline of " + body.name
        outline.short_name = body.short_name
        outline.is_official = False
    else:
        outline = body.outline

    query = query_template_outline.format(gemeindeschluessel)

    response = requests.post(overpass_api, data={"data": query})
    response.raise_for_status()
    geojson = osm2geojson.json2geojson(response.text)
    outline.geometry = geojson
    outline.save()

    body.outline = outline
    body.save()
Esempio n. 8
0
 def test_issue_9(self):
     (data, saved_geojson) = get_json_and_geojson_data('issue-9')
     all_geojson = json.loads(read_data_file('issue-9-all.geojson'))
     self.assertDictEqual(saved_geojson, json2geojson(data))
     self.assertDictEqual(all_geojson,
                          json2geojson(data, filter_used_refs=False))
Esempio n. 9
0
 def test_issue_7(self):
     (data, saved_geojson) = get_json_and_geojson_data('issue-7')
     self.assertDictEqual(saved_geojson, json2geojson(data))
    response = requests.get(overpass_url, params={'data': overpass_query})
    osmjson = response.json()
    with open(cache_file, 'w', encoding='utf8') as json_file:
        json.dump(osmjson, json_file, ensure_ascii=False)

    print("fetching from overpass done")
else:
    print("reading from cache")
    with open(cache_file, 'r') as myfile:
        osmjson = json.load(myfile)
    print("reading from cache done")

#convert overpass json to geojson
import osm2geojson
print("converting to geojson")
osmdata = osm2geojson.json2geojson(osmjson)
print("conversion done")
#print(osmdata["features"][0])

matches = set()
print("Searching for matches within 100m")
# loop through the list
for osmfeature in osmdata["features"]:
    osmfeature_count += 1
    # debug break out
    # if (osmfeature_count == 5):
    #     break
    osmid = osmfeature["properties"]["id"]
    lat = osmfeature["geometry"]["coordinates"][1]
    lon = osmfeature["geometry"]["coordinates"][0]
    osmcoord = (lat, lon)
Esempio n. 11
0
import shapely.wkb

drs, shapefile_name, boundsfile_name = sys.argv[1:]
overpass_url = "http://overpass-api.de/api/interpreter"
overpass_query = """
[out:json];
area["ISO3166-1"="DE"];
rel["de:regionalschluessel"="%s"](area);
out geom;
""" % drs
# 059130000000 Regionalschl. Dortmund

response = requests.get(overpass_url, params={'data': overpass_query})
try:
    data = response.json()
except:
    print(response.content)
    raise

geos = osm2geojson.json2geojson(data)
print(geos)
fullgeom = shapely.ops.unary_union(
    [shapely.geometry.shape(geo['geometry']) for geo in geos['features']])
print(fullgeom)

with open(shapefile_name, 'wb') as shapefile:
    shapefile.write(shapely.wkb.dumps(fullgeom))

with open(boundsfile_name, 'w') as boundsfile:
    boundsfile.write(' '.join(str(x) for x in fullgeom.bounds))
Esempio n. 12
0
 def _as_geojson(self, response):
     return osm2geojson.json2geojson(response)
Esempio n. 13
0
 def test_center_feature(self):
     (data, saved_geojson) = get_json_and_geojson_data('center-feature')
     self.assertDictEqual(saved_geojson, json2geojson(data))
Esempio n. 14
0
def overpass_to_geojson(
    output_file: str,
    area_id: int,
    out: str = "center",  # center, body, geom
    response_type: str = "json",
    overpass_endpoint: list[str] = OVERPASS_ENDPOINTS[0],
    force_download=False,
    **kwargs,
):
    today = date.today()
    output_file_path = Path(output_file)
    tags_to_download = "".join(f'["{key}"="{value}"]'
                               for key, value in kwargs.items())

    if response_type not in ["json", "xml"]:
        return None

    try:
        file_last_mod_date = datetime.fromtimestamp(
            output_file_path.stat().st_mtime).date()
    except FileNotFoundError:
        file_last_mod_date = date(1900, 1, 1)

    if (output_file_path.is_file() and file_last_mod_date == today
            and force_download is False):
        logging.info(
            f"Finish: File is up to date. (generated: {file_last_mod_date})")
        return None

    # 2. Step 2 - connecting and getting data from Overpass
    else:
        logging.info(
            f"Info: Export .geojson file last modification date: {file_last_mod_date}"
        )
        # Overpass Query
        compact_query = f"[out:{response_type}][timeout:20005];area({area_id})->.searchArea;(node{tags_to_download}(area.searchArea);way{tags_to_download}(area.searchArea);relation{tags_to_download}(area.searchArea););out {out};"
        query = overpass_endpoint + "?data=" + compact_query
        logging.info(
            f"Start: Connecting to Overpass server: {overpass_endpoint}")
        try:
            response = requests.get(query)
            response.raise_for_status()
            pass
        except requests.exceptions.HTTPError as err:
            raise SystemExit(err)
        if response.status_code != 200:
            logging.error("End: Server response other than 200")
            return None
        try:
            logging.info(
                "Start: Getting data and extracting to .geojson object..")
            if response_type == "json":
                geojson_response = json2geojson(response.text,
                                                log_level="ERROR")
            else:
                geojson_response = xml2geojson(response.text,
                                               log_level="ERROR")
        except:
            logging.error(
                "Finish: Error when converting response .json to .geojson")
            return None

        with open(output_file_path, mode="w", encoding="utf-8") as f:
            geojson.dump(geojson_response, f)
            logging.info(
                "Finish: GeoJSON object successfully dumped to .geojson file")
            return True
Esempio n. 15
0
import json
from os import path, rename
from osm2geojson import json2geojson, overpass_call
import pycountry
import time

admin_level = 2

for c in pycountry.countries:
    filename = f'{c.alpha_3}.geojson'
    if path.exists(f'data/2/{filename}') or path.exists(
            f'data/3/{filename}') or path.exists(
                f'data/4/{filename}') or path.exists(f'data/6/{filename}'):
        continue
    print(c)
    # alternatively: don't filter for admin_level, but check if it's 2 (country) or 3 (overseas something)
    # filter out boundary=claimed_administrative
    # land masses or boundary=claimed_administrative areas are sometimes also tagged with the ISO codes, so best to ask for admin_level explicitly AND set type != land_area
    r = json.loads(
        overpass_call(
            f'[out:json];(rel["ISO3166-1:alpha2"={c.alpha_2}];rel["ISO3166-1:alpha3"={c.alpha_3}];)->.a;rel.a[boundary=administrative][admin_level={admin_level}][type!=land_area];out geom;'
        ))
    if len(r['elements']) == 0:
        print('empty result!')
        continue
    filename = f'data/{r["elements"][0]["tags"]["admin_level"]}/{filename}'
    print(f'writing to {filename}')
    with open(filename, 'w') as f:
        json.dump(json2geojson(r), f)
def get_data_from_overpass(query):
    endpoint = "https://overpass-api.de/api/interpreter"
    response = requests.get(endpoint, params={'data': query})
    overpass_json_data = response.json()
    geojson_data = json2geojson(overpass_json_data)
    return geojson_data
Esempio n. 17
0
 def test_meta_tags(self):
     (data, saved_geojson) = get_json_and_geojson_data('meta')
     self.assertDictEqual(saved_geojson, json2geojson(data))