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 location(self, libobject: OParl.Location): location, do_update = self.check_for_modification( libobject, Location, name_fixup=_("Unknown")) if not location or not do_update: return location self.logger.info("Processing Location {}".format(libobject.get_id())) location.oparl_id = libobject.get_id() location.description = libobject.get_description() location.is_official = self.official_geojson location.geometry = self.extract_geometry(libobject.get_geojson()) location.streetAddress = libobject.get_street_address() location.room = libobject.get_room() location.postalCode = libobject.get_postal_code() location.locality = libobject.get_locality() # Try to guess a better name for the location if libobject.get_room(): location.short_description = libobject.get_room() if not location.description: description = "" if libobject.get_room(): description += libobject.get_room() + ", " if libobject.get_street_address(): description += libobject.get_street_address() + ", " if libobject.get_locality(): if libobject.get_postal_code(): description += libobject.get_postal_code() + " " description += libobject.get_locality() location.description = description # If a streetAddress is present, we try to find the exact location on the map if location.streetAddress: search_str = libobject.get_street_address() + ", " if libobject.get_locality(): if libobject.get_postal_code(): search_str += libobject.get_postal_code() + " " search_str += libobject.get_locality() else: search_str += settings.GEOEXTRACT_DEFAULT_CITY search_str += " " + settings.GEO_SEARCH_COUNTRY geodata = geocode(search_str) if geodata: location.geometry = { "type": "Point", "coordinates": [geodata['lng'], geodata['lat']] } location.save() return location
def handle(self, *args, **options): search_str = options["search_str"] geometry = geocode(search_str) if not geometry: raise CommandError("Geocoding failed") elif geometry["type"] == "Point": logger.info(f"Success! Geometry for '{search_str}': {geometry}") else: raise CommandError( f"Wrong geometry returned for '{search_str}': {geometry}" )
def extract_locations( text: str, fallback_city: Optional[str], pipeline: Optional[AddressPipeline] = None) -> List[Location]: if not text: return [] if not fallback_city: fallback_city = Body.objects.get( id=settings.SITE_DEFAULT_BODY).short_name if not pipeline: pipeline = AddressPipeline(create_geoextract_data()) if len(text) < settings.TEXT_CHUNK_SIZE: found_locations = pipeline.extract(text) else: # Workaround for https://github.com/stadt-karlsruhe/geoextract/issues/7 found_locations = [] for i in range(0, len(text), settings.TEXT_CHUNK_SIZE): # We can't use set because the dicts in the returned list are unhashable for location in pipeline.extract(text[i:i + settings.TEXT_CHUNK_SIZE]): if location not in found_locations: found_locations.append(location) locations = [] for found_location in found_locations: if "name" in found_location and len(found_location["name"]) < 5: continue location_name = format_location_name(found_location) search_str = get_search_string(found_location, fallback_city) defaults = { "description": location_name, "is_official": False, # This cutoff comes from a limitation of InnoDB "search_str": search_str[:767], } # Avoid "MySQL server has gone away" errors due to timeouts # https://stackoverflow.com/a/32720475/3549270 db.close_old_connections() location, created = Location.objects_with_deleted.get_or_create( search_str=search_str, defaults=defaults) if created: location.geometry = geocode(search_str) location.save() locations.append(location) return locations
def get_geodata(location, fallback_city_name): search_str = '' if 'street' in location: search_str += location['street'] if 'house_number' in location: search_str += ' ' + location['house_number'] if 'postcode' in location: search_str += ', ' + location['postcode'] + ' ' + location['city'] elif 'city' in location: search_str += ', ' + location['city'] else: search_str += ', ' + fallback_city_name elif 'name' in location: search_str += location['name'] + ', ' + fallback_city_name search_str += ', ' + settings.GEO_SEARCH_COUNTRY return geocode(search_str)