def fix_crime_geom(): qs = NewsItem.objects.filter(schema__slug="crime", location__isnull=False) count = qs.count() for i, ni in enumerate(qs.iterator()): print "# => Checking %s of %s" % (i, count) x, y = [float(n) for n in ni.attributes["xy"].split(";")] pt = Point((x, y)) pt.srid = 4326 location_name = ni.location_name.replace("XX", "01") try: result = geocoder.geocode(location_name) except (GeocodingException, ParsingError): print " Could not geocode" NewsItem.objects.filter(id=ni.id).update(location=None) else: is_close, distance = locations_are_close(ni.location, pt, THRESHOLD) if not is_close: print " Too far: %s" % distance NewsItem.objects.filter(id=ni.id).update(location=None) else: print " Fine"
def safe_location(self, location_name, geom, max_distance=200): """ Returns a location (geometry) to use, given a location_name and geometry. This is used for data sources that publish both a geometry and a location_name -- we double-check that the geometry is within a certain `max_distance` from the geocoded location_name. If there's a discrepancy or if the location_name can't be geocoded, this returns None. """ location = self.geocode(location_name) if location is None: return None location_point = location['point'] if not location_point: return None location_point.srid = 4326 is_close, distance = locations_are_close(location_point, geom, max_distance) if not is_close: return None return geom
def fix_crime_geom(): qs = NewsItem.objects.filter(schema__slug='crime', location__isnull=False) count = qs.count() for i, ni in enumerate(qs.iterator()): print '# => Checking %s of %s' % (i, count) x, y = [float(n) for n in ni.attributes['xy'].split(';')] pt = Point((x, y)) pt.srid = 4326 location_name = ni.location_name.replace('XX', '01') try: result = geocoder.geocode(location_name) except (GeocodingException, ParsingError): print ' Could not geocode' NewsItem.objects.filter(id=ni.id).update(location=None) else: is_close, distance = locations_are_close(ni.location, pt, THRESHOLD) if not is_close: print ' Too far: %s' % distance NewsItem.objects.filter(id=ni.id).update(location=None) else: print ' Fine'