def create_location(self, zipcode, geom, display_order=0): verbose = self.verbose source = self.source geom = ensure_valid(geom, self.location_type.slug) geom = flatten_geomcollection(geom) geom.srid = 4326 kwargs = dict( name = zipcode, normalized_name = zipcode, slug = zipcode, location_type = self.location_type, location = geom, city = self.metro_name, source = source, is_public = True, ) if not self.should_create_location(kwargs): return kwargs['defaults'] = {'display_order': display_order, 'last_mod_date': self.now, 'creation_date': self.now, 'area': kwargs['location'].transform(3395, True).area, } zipcode_obj, created = Location.objects.get_or_create(**kwargs) if verbose: print >> sys.stderr, '%s ZIP Code %s ' % (created and 'Created' or 'Already had', zipcode_obj.name) return created
def create_location(self, zipcode, geom, display_order=0): verbose = self.verbose source = self.source geom = ensure_valid(geom, self.location_type.slug) geom = flatten_geomcollection(geom) geom.srid = 4326 kwargs = dict( name=zipcode, normalized_name=zipcode, slug=zipcode, location_type=self.location_type, location=geom, city=self.metro_name, source=source, is_public=True, ) if not self.should_create_location(kwargs): return kwargs["defaults"] = { "display_order": display_order, "last_mod_date": self.now, "creation_date": self.now, "area": kwargs["location"].transform(3395, True).area, } zipcode_obj, created = Location.objects.get_or_create(**kwargs) if verbose: print >>sys.stderr, "%s ZIP Code %s " % (created and "Created" or "Already had", zipcode_obj.name) return created
def save(self, name_field): verbose = self.verbose source = self.source locs = [] for feature in self.layer: name = feature.get(name_field) geom = feature.geom.transform(4326, True).geos geom = ensure_valid(geom, name) geom = flatten_geomcollection(geom) fields = dict( name = name, slug = slugify(name), location_type = self.get_location_type(feature), location = geom, city = self.metro_name, source = source, is_public = True, ) if not self.should_create_location(fields): continue locs.append(fields) num_created = 0 for i, loc_fields in enumerate(sorted(locs, key=lambda h: h['name'])): kwargs = dict( loc_fields, defaults={ 'creation_date': self.now, 'last_mod_date': self.now, 'display_order': i, 'normalized_name': normalize(loc_fields['name']), 'area': loc_fields['location'].transform(3395, True).area, }) try: loc, created = Location.objects.get_or_create(**kwargs) except IntegrityError: # Usually this means two towns with the same slug. # Try to fix that. slug = kwargs['slug'] existing = Location.objects.filter(slug=slug).count() if existing: slug = slugify('%s-%s' % (slug, existing + 1)) if verbose: print >> sys.stderr, "Munged slug %s to %s to make it unique" % (kwargs['slug'], slug) kwargs['slug'] = slug loc, created = Location.objects.get_or_create(**kwargs) else: raise if created: num_created += 1 if verbose: print >> sys.stderr, '%s %s %s' % (created and 'Created' or 'Already had', self.location_type.name, loc) if verbose: sys.stderr.write('Populating newsitem locations ... ') populate_ni_loc(loc) if verbose: sys.stderr.write('done.\n') return num_created
def create_location(self, name, location_type, geom, display_order=0): source = self.source if hasattr(geom, 'geos'): geom = geom.geos if geom.srid is None: geom.srid = 4326 elif geom.srid != 4326: geom = geom.transform(4326, True) geom = ensure_valid(geom, name) geom = flatten_geomcollection(geom) if not isinstance(location_type, int): location_type = location_type.id kwargs = dict( name=name, slug=slugify(name), location=geom, location_type_id=location_type, city=self.metro_name, source=source, is_public=True, ) if not self.should_create_location(kwargs): return kwargs['defaults'] = { 'creation_date': self.now, 'last_mod_date': self.now, 'display_order': display_order, 'normalized_name': normalize(name), 'area': geom.transform(3395, True).area, } try: loc, created = Location.objects.get_or_create(**kwargs) except IntegrityError: # Usually this means two towns with the same slug. # Try to fix that. slug = kwargs['slug'] existing = Location.objects.filter(slug=slug).count() if existing: slug = slugify('%s-%s' % (slug, existing + 1)) logger.info("Munged slug %s to %s to make it unique" % (kwargs['slug'], slug)) kwargs['slug'] = slug loc, created = Location.objects.get_or_create(**kwargs) else: raise logger.info('%s %s %s' % (created and 'Created' or 'Already had', self.location_type.name, loc)) logger.info('Populating newsitem locations ... ') populate_ni_loc(loc) logger.info('done.\n') return created
def create_location(self, name, location_type, geom, display_order=0): source = self.source geom = geos_with_projection(geom, 4326) geom = ensure_valid(geom, name) geom = flatten_geomcollection(geom) if not isinstance(location_type, int): location_type = location_type.id kwargs = dict( name=name, slug=slugify(name), location=geom, location_type_id=location_type, city=self.metro_name, source=source, is_public=True, ) if not self.should_create_location(kwargs): return kwargs['defaults'] = { 'creation_date': self.now, 'last_mod_date': self.now, 'display_order': display_order, 'normalized_name': normalize(name), 'area': geom.transform(3395, True).area, } try: loc, created = Location.objects.get_or_create(**kwargs) except IntegrityError: # Usually this means two towns with the same slug. # Try to fix that. slug = kwargs['slug'] existing = Location.objects.filter(slug=slug).count() if existing: slug = slugify('%s-%s' % (slug, existing + 1)) logger.info("Munged slug %s to %s to make it unique" % (kwargs['slug'], slug)) kwargs['slug'] = slug loc, created = Location.objects.get_or_create(**kwargs) else: raise logger.info('%s %s %s' % (created and 'Created' or 'Already had', self.location_type.name, loc)) logger.info('Populating newsitem locations ... ') populate_ni_loc(loc) logger.info('done.\n') return created
def import_county(self): county_type_data = { 'name': 'County', 'plural_name': 'Counties', 'slug': 'counties', 'is_browsable': True, 'is_significant': False, 'scope': self.metro_name, } try: county_type = LocationType.objects.get(slug=county_type_data['slug']) except LocationType.DoesNotExist: county_type = LocationType.objects.create(**county_type_data) Location.objects.filter(location_type=county_type).delete() county_layer = DataSource('%s/%s.shp' % (self.zip_dir, self.datafiles['county']['file_name']))[0] now = datetime.datetime.now() county_location = None for feature in county_layer: if feature.get('GEOID10') == self.county: name = feature.get(self.datafiles['county']['name_field']) geom = feature.geom.transform(4326, True).geos geom = ensure_valid(geom, name) geom = flatten_geomcollection(geom) loc_fields = dict( name = name, slug = slugify(name), location_type = county_type, location = geom, city = self.metro_name, is_public = True, ) kwargs = dict( loc_fields, ) kwargs.update({ 'creation_date': now, 'last_mod_date': now, 'display_order': 0, 'normalized_name': normalize(loc_fields['name']), 'area': loc_fields['location'].transform(3395, True).area, }) county_location = Location.objects.create(**kwargs) break return county_location
def clean(self): self.location = ensure_valid(flatten_geomcollection(self.location))
def clean(self): if self.location: try: self.location = ensure_valid(flatten_geomcollection(self.location)) except ValueError, e: raise ValidationError(str(e))