Exemplo n.º 1
0
    def test_overpass_queries(self):
        """
        Test several queries to overpass
        """
        queries = [
            'rel(448930); out geom;',
            'rel(448930); out meta;',
            'rel(448930); out bb;',
            'rel(448930); out count;'
        ]

        for query in queries:
            print('Test query:', query)
            query_json = '[out:json];' + query
            data_xml = overpass_call(query)
            data_json = overpass_call(query_json)
            overpass_json = json.loads(data_json)
            parsed_json = parse_xml(data_xml)
            # Ignore different time for queries
            del overpass_json['osm3s']['timestamp_osm_base']
            del parsed_json['osm3s']['timestamp_osm_base']
            # Ignore generator (overpass use different versions of generators?)
            del overpass_json['generator']
            del parsed_json['generator']
            self.assertDictEqual(overpass_json, parsed_json)
Exemplo n.º 2
0
 def test_parsing_from_overpass(self):
     """
     Test city border convertation to MultiPolygon
     """
     xml = overpass_call('rel(448930); out geom;')
     data = xml2geojson(xml)
     self.assertEqual(len(data['features']), 1)
Exemplo n.º 3
0
def process_request(payload):
    coords = payload['coords']
    main_object = payload['mainObject']
    relative_object = payload['relativeObject']

    poly_coords = None

    query = ""

    request_begin = time.time()

    # search considering time reach distance
    if main_object['timeReachOn']:
        time_distance = main_object['maxDistance'] * 60
        poly_coords = get_opr_isochrone_poly_coords(coords, time_distance, main_object['transportMean'])

    # search considering also relative object
    if relative_object['applicable']:
        query = get_ovp_main_relative_object_search_query(main_object, relative_object, coords, poly_coords)

    # search only by main object
    else:
        query = get_ovp_main_object_search_query(main_object, coords, poly_coords)

    query += "(._;>;);out body;"
    # logger.info(query)

    try:
        data = o2g.xml2geojson(o2g.overpass_call(query), filter_used_refs=True, log_level='INFO')

        objects_number = len(data['features'])

        data = process_overpass_data(data)

        response_time = round(time.time() - request_begin, 2)
        logger.info('request time: ' + str(response_time) + 's; elements number: ' + str(objects_number))

        return {
            'geojson': data,
            'polygon': poly_coords,
            'time': response_time,
            'objectsNumber': objects_number
        }
    except Exception as e:
        msg = 'Error processing request. Try again. Error: ' + str(e)
        logger.error(msg)

    return { 'error': msg }
Exemplo n.º 4
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)