示例#1
0
    def location(self, libobject: JSON, location: Location) -> Location:
        location.description = libobject.get("description")
        location.is_official = self.utils.official_geojson
        location.geometry = libobject.get("geojson", {}).get("geometry")

        location.street_address = libobject.get("streetAddress")
        location.room = libobject.get("room")
        location.postal_code = libobject.get("postalCode")
        location.locality = libobject.get("locality")

        if not location.description:
            description = ""
            if location.room:
                description += location.room + ", "
            if location.street_address:
                description += location.street_address + ", "
            if location.locality:
                if location.postal_code:
                    description += location.postal_code + " "
                description += location.locality
            location.description = description

        # If a street_address is present, we try to find the exact location on the map
        if location.street_address and not location.geometry:
            search_str = location.street_address + ", "
            if location.locality:
                if location.postal_code:
                    search_str += location.postal_code + " " + location.locality
            elif self.default_body:
                search_str += self.default_body.short_name
            search_str += " " + settings.GEOEXTRACT_SEARCH_COUNTRY

            location.geometry = geocode(search_str)

        return location
    def test_location(self):
        location = Location()
        libobject = self.api_data["https://oparl.example.org/location/0"]
        self.converter.location(libobject, location)
        self.assertEqual(
            location.description,
            "Deutscher Bundestag, Platz der Republik 1, 11011 Berlin",
        )

        self.assertEqual(location.street_address, "Platz der Republik 1")
        self.assertEqual(location.postal_code, "11011")
        self.assertEqual(location.locality, "Berlin")
        self.assertEqual(location.room, "Plenarsaal")

        self.assertTrue(location.is_official)
        self.assertAlmostEqual(location.coordinates()["lat"], 52.518855)
        self.assertAlmostEqual(location.coordinates()["lon"], 13.376198)
示例#3
0
def extract_locations(text, fallback_city=None):
    """
    :type text: str
    :type fallback_city: str
    :return: list of mainapp.models.Body
    """
    if not fallback_city:
        fallback_city = settings.GEOEXTRACT_DEFAULT_CITY

    found_locations = extract_found_locations(text)

    locations = []
    for found_location in found_locations:
        location_name = format_location_name(found_location)
        try:
            location = Location.objects.get(name=location_name)
            locations.append(location)
        except Location.DoesNotExist:
            geodata = get_geodata(found_location, fallback_city)

            location = Location()
            location.name = location_name
            location.short_name = location_name
            location.is_official = False
            location.osm_id = None  # @TODO
            if geodata:
                location.geometry = {
                    "type": "Point",
                    "coordinates": [geodata['lng'], geodata['lat']]
                }
            else:
                location.geometry = None
            location.save()

            bodies = detect_relevant_bodies(location)
            for body in bodies:
                location.bodies.add(body)

            locations.append(location)

    return locations
    def test_location_default_body(self):
        """ Test that the default body is used when locality isn't given """
        city_center = {"coordinates": [48.1375, 11.575833], "type": "Point"}

        def geocode_city_center(search_str: str) -> dict:
            if search_str == "Marienplatz 1, München Deutschland":
                return city_center
            else:
                raise AssertionError(search_str)

        with mock.patch("importer.json_to_db.geocode",
                        new=geocode_city_center):
            location = Location()
            libobject = {"streetAddress": "Marienplatz 1"}
            self.converter.location(libobject, location)
            self.assertEqual(location.geometry, city_center)
示例#5
0
def import_outline(body: Body, gemeindeschluessel: str):
    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

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

    query = query_template_outline.format(gemeindeschluessel)

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

    body.outline = outline
    body.save()
示例#6
0
def import_outline(body, 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

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

    query = query_template_outline.format(gemeindeschluessel)

    r = requests.post(overpass_api, data={'data': query})

    geojson = convert_to_geojson(r.text)
    outline.geometry = geojson
    outline.save()

    body.outline = outline
    body.save()
def import_outline(body: Body, ags: Optional[str] = None):
    ags = ags or body.ags
    assert ags is not None

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

    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 = format_template(query_template_outline, ags)

    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()