コード例 #1
0
    def perform_check(self, layerdefinition):
        """Performs check and returns geojson collectioin of results found.
          The param layerdefinition is expected as a dict declaring a property "overpass_query" which
          is the raw overpass query to be sent to the overpass-api server.
          The query may contain placeholders (for now only {{bbox}}, which will be replaced
          by self.area_or_boundingbox.
        """

        query = layerdefinition['overpass_query']

        bbQuery = BoundingBoxQuery(query, self.area_or_boundingbox)
        logger.debug('Retrieve overpass layer via query %s', bbQuery)
        # overpass-wrapper currently does not support custom settings, so build=False
        response = self.api.get(bbQuery, 'json', build=False)
        geojson_response = osmtogeojson.process_osm_json(response)

        return geojson_response
コード例 #2
0
    def run(self, query, name_de, name_en, iconId, iconSourceFile):
        default_name = name_de
        layerName = name_en.replace(" ", "").replace("/", "").lower()
        filePath = self.dest_dir + layerName + '.geojson'
        iconSource = self.read_svg_source(iconSourceFile)

        bbQuery = self._prepare_overpass_query(query, self.bbox)
        print(bbQuery)

        response = self.api.get(bbQuery, 'geojson', build=False)
        #print(response)

        data = osmtogeojson.process_osm_json(response)
        #self.write_geojson_file(geojson_response, filePath)

        #data = self._load_json_file(filePath)
        self.add_properties(data, default_name, name_de, name_en)
        self.localize_description(data)
        self.set_default_description(data)
        self.add_icon(data, iconId, iconSource)
        self.delete_unnecessary_properties(data, self.necessary_properties)
        self._write_geojson_file(data, filePath)
コード例 #3
0
ファイル: osm.py プロジェクト: luftj/MaRE
def get_from_osm(bbox=[16.3, 54.25, 16.834, 54.5], url=osm_url):
    os.makedirs(path_osm, exist_ok=True)
    data_path = path_osm + "rivers_%s.geojson" % "_".join(map(str, bbox))

    if proj_sheets != proj_osm:  # reproject sheet bounding box to OSM coordinates
        minxy = transform_sheet_to_osm.transform(
            bbox[0], bbox[1])  # reproject lower left bbox corner
        maxxy = transform_sheet_to_osm.transform(
            bbox[2], bbox[3])  # reproject upper right bbox corner
        bbox = minxy + maxxy

    # prepare polygon files of ocean cover
    ocean_file_path = "%s/water_poly_%s.geojson" % (path_osm, "-".join(
        map(str, bbox)))
    if draw_ocean_polygon and not os.path.isfile(ocean_file_path):
        clip_ocean_poly(bbox)

    try:
        # don't query if we already have the data on disk
        if not force_osm_download and os.path.isfile(data_path):
            logging.debug("fetching osm data from disk: %s" % data_path)
            with open(data_path, encoding="utf-8") as file:
                json_data = json.load(file)
                return json_data
    except JSONDecodeError:
        print("error in OSM data on disk, trying redownlaoding...")

    sorted_bbox = ",".join(map(str, [bbox[1], bbox[0], bbox[3], bbox[2]]))
    query = osm_query.replace("{{bbox}}", "%s" % sorted_bbox)
    logging.debug("osm query: %s" % query)

    while True:
        try:
            result = requests.get(url,
                                  params={'data': query},
                                  timeout=download_timeout)
            result = result.json()
            break
        except Exception as e:
            import re
            error_msg = re.findall("error[^<]*", result.text)
            if len(error_msg) == 0:
                logging.critical(result.text)
                print(result.text)
                raise (e)
            logging.error(error_msg)
            if "rate_limited" in error_msg[0] or "timeout" in error_msg[0]:
                logging.warning(
                    "timeout or rate limited, retrying in 5 sec...")
                sleep(5)
                continue
            else:
                print("unknown error" + result.text)
                logging.critical("unknown error" + result.text)
                raise (e)
    gj = osmtogeojson.process_osm_json(result)

    with open(data_path, mode="w", encoding="utf-8") as f:
        json.dump(gj, f)

    return gj
コード例 #4
0
  (
    // query part for: “building=yes”
    node["building"="yes"](area.searchArea)(area.externalBoundary);
    way["building"="yes"](area.searchArea)(area.externalBoundary);
    relation["building"="yes"](area.searchArea)(area.externalBoundary);
  );
  // print results
  out body;
  >;
  out skel qt;
  """.format(comune)
    print(building_query)
    r = requests.get(url, params={'data': building_query})
    # res = api.get(building_query)
    try:
        result = osmtogeojson.process_osm_json(r.json())
        comune_clean = str(re.sub('[^\w\s-]', '', comune).strip().lower())
        comune_clean = str(re.sub('[-\s]+', '-', comune_clean))
        comune_clean = unicodedata.normalize('NFKD', comune_clean).encode(
            'ascii', 'ignore')
        comune_clean = comune_clean.decode('utf-8')
        filepath = os.path.join('./building',
                                "{}.geo.json".format(comune_clean))
        print(filepath)
        with open(filepath, mode="w") as f:
            geojson.dump(result, f)
    except e:
        print("Error during the extraction of comune {}".format(comune))
        print(r.content)
    time.sleep(5)
コード例 #5
0
import json
from osmtogeojson.osmtogeojson import process_osm_json

#f = open("tests/fixtures/summitschool_overpass.json", "r").read()
f = open("tests/fixtures/np_overpass.json", "r").read()
j = json.loads(f)

resulting_geojson = process_osm_json(j)

#with open("tests/fixtures/summitschool_geojson.json", "r") as f:
with open("tests/fixtures/np_geojson.json", "r") as f:
    gj = json.loads(f.read())

gj_ids = {}
for f in gj["features"]:
    gj_ids[f["id"]] = f

my_ids = {}
for f in resulting_geojson["features"]:
    my_ids[f["id"]] = f

total = 0
number_worked = 0
print("in their not in mine")
print([x for x in gj_ids if x not in my_ids])
print("in mine not in theirs")
print([x for x in my_ids if x not in gj_ids])
print("\n")
for f in [x for x in gj_ids if x in my_ids]:
    total += 1
    if gj_ids[f] == my_ids[f]: