def recalculate_bounds(self): primary_geography_feature_classes = [self.db_entity_feature_class(db_entity.key) for db_entity in self.owned_db_entities() if get_property_path(db_entity, 'feature_class_configuration.primary_geography')] use_for_bounds_feature_classes = [self.db_entity_feature_class(db_entity.key) for db_entity in self.owned_db_entities() if get_property_path(db_entity, 'feature_class_configuration.use_for_bounds')] authority_feature_classes = use_for_bounds_feature_classes if len(use_for_bounds_feature_classes) > 0 \ else primary_geography_feature_classes extents = [] for authority_feature_class in authority_feature_classes: all_features = authority_feature_class.objects.all() if len(all_features) > 0: bounds = all_features.extent_polygon() extents.append(bounds) self.bounds = MultiPolygon(extents) # Disable publishers for this simple update self._no_post_save_publishing = True self.save() self._no_post_save_publishing = False else: pass
def recalculate_bounds(self): primary_geography_feature_classes = [ self.db_entity_feature_class(db_entity.key) for db_entity in self.owned_db_entities() if get_property_path( db_entity, 'feature_class_configuration.primary_geography') ] use_for_bounds_feature_classes = [ self.db_entity_feature_class(db_entity.key) for db_entity in self.owned_db_entities() if get_property_path( db_entity, 'feature_class_configuration.use_for_bounds') ] authority_feature_classes = use_for_bounds_feature_classes if len(use_for_bounds_feature_classes) > 0 \ else primary_geography_feature_classes extents = [] for authority_feature_class in authority_feature_classes: all_features = authority_feature_class.objects.all() if len(all_features) > 0: bounds = all_features.extent_polygon() extents.append(bounds) self.bounds = MultiPolygon(extents) # Disable publishers for this simple update self._no_post_save_publishing = True self.save() self._no_post_save_publishing = False else: pass
def client_instances(self): """ If client_instance_path is specified these are the mapped instances that the client cares about :return: """ return map( lambda instance: get_property_path(instance, self.client_instance_path) if\ self.client_instance_path else\ instance, self.instances)
def properties_have_changed(self, property_dict, *properties): return any_true(lambda property: normalize_null(get_property_path(self, property)) != normalize_null(get_property_path(property_dict, property)), properties)
def client_instance_ids(self): return map( lambda instance: get_property_path(instance, self.client_instance_path).id if\ self.client_instance_path else\ instance.id, self.instances)
def default_db_entities(self, **kwargs): """ Region specific SACOG db_entity_setups :param default_dict: :return: """ config_entity = self.config_entity parent_region_fixture = self.parent_fixture default_db_entities = parent_region_fixture.default_db_entities() Key = SacogDbEntityKey # The Behavior keyspace behavior_key = BehaviorKey.Fab.ricate # Used to load Behaviors defined elsewhere get_behavior = lambda key: Behavior.objects.get(key=behavior_key(key)) if self.config_entity.key == 'sacog': # Block the client-level region. We just want real regions return [] remote_db_entity_setups = FixtureList( map( lambda db_entity: update_or_create_db_entity( config_entity, db_entity), self.default_remote_db_entities())) # TODO: Disabled the 'sacog_background' 'remote db entity' b/c it is throwing errors (2016-07-06) # return default_db_entities + remote_db_entity_setups + FixtureList([ return default_db_entities + FixtureList([ update_or_create_db_entity( config_entity, DbEntity( key=DbEntityKey.REGION_PRIMARY_GEOGRAPHY, # Override. If a name override is supplied, put it in. Otherwise leave null to derive it from the key name='Urban Base Canvas (UF)', # name=get_property_path(kwargs, 'overrides.%s.name' % Key.BASE), feature_class_configuration=FeatureClassConfiguration( abstract_class=CanvasFeature, # The Base Feature is normally considered a primary_geography unless overridden primary_geography=True, primary_key='geography_id', primary_key_type='int', # The Base Feature is normally associated to a subclass of Geography unless overridden geography_class_name=get_property_path( kwargs, 'overrides.%s.geography_class_name' % Key.BASE_CANVAS) or 'footprint.main.models.geographies.parcel.Parcel', # Create a built_form ForeignKey to a single BuiltForm, # by initially joining our 'built_form_key' attribute to its 'key' attribute related_fields=dict(built_form=dict( single=True, related_class_name=uf_model( 'built_form.built_form.BuiltForm'), source_class_join_field_name='built_form_key', related_class_join_field_name='key', ))), feature_behavior=FeatureBehavior( behavior=get_behavior('base_feature'), ), _categories=[ Category(key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( config_entity, DbEntity(key=Key.CENSUS_TRACTS, feature_class_configuration=FeatureClassConfiguration( abstract_class=CensusTract), feature_behavior=FeatureBehavior( behavior=get_behavior('reference'), intersection=GeographicIntersection. polygon_to_polygon), _categories=[ Category( key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( config_entity, DbEntity(key=Key.CENSUS_BLOCKGROUPS, feature_class_configuration=FeatureClassConfiguration( abstract_class=CensusBlockgroup), feature_behavior=FeatureBehavior( behavior=get_behavior('reference'), intersection=GeographicIntersection. polygon_to_centroid), _categories=[ Category( key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( config_entity, DbEntity(key=Key.CENSUS_BLOCK, feature_class_configuration=FeatureClassConfiguration( abstract_class=CensusBlock), feature_behavior=FeatureBehavior( behavior=get_behavior('reference'), intersection=GeographicIntersection. polygon_to_centroid), _categories=[ Category( key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( config_entity, DbEntity(key=Key.LIGHT_RAIL, feature_class_configuration=FeatureClassConfiguration( abstract_class=SacogLightRailFeature), feature_behavior=FeatureBehavior( behavior=get_behavior('reference'), intersection=GeographicIntersection. polygon_to_polygon), _categories=[ Category( key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( config_entity, DbEntity(key=Key.LIGHT_RAIL_STOPS, feature_class_configuration=FeatureClassConfiguration( abstract_class=SacogLightRailStopsFeature), feature_behavior=FeatureBehavior( behavior=get_behavior('reference'), intersection=GeographicIntersection. polygon_to_polygon), _categories=[ Category( key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( config_entity, DbEntity( key=Key.LIGHT_RAIL_STOPS_ONE_MILE, feature_class_configuration=FeatureClassConfiguration( abstract_class=SacogLightRailStopsOneMileFeature, ), feature_behavior=FeatureBehavior( behavior=get_behavior('reference'), intersection=GeographicIntersection.polygon_to_polygon ), _categories=[ Category(key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( config_entity, DbEntity( key=Key.LIGHT_RAIL_STOPS_HALF_MILE, feature_class_configuration=FeatureClassConfiguration( abstract_class=SacogLightRailStopsHalfMileFeature), feature_behavior=FeatureBehavior( behavior=get_behavior('reference'), intersection=GeographicIntersection.polygon_to_polygon ), _categories=[ Category(key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( config_entity, DbEntity( key=Key.LIGHT_RAIL_STOPS_QUARTER_MILE, feature_class_configuration=FeatureClassConfiguration( abstract_class=SacogLightRailStopsQuarterMileFeature), feature_behavior=FeatureBehavior( behavior=get_behavior('reference'), intersection=GeographicIntersection.polygon_to_polygon ), _categories=[ Category(key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( config_entity, DbEntity( key=Key.VERNAL_POOL, feature_class_configuration=FeatureClassConfiguration( abstract_class=SacogVernalPoolFeature), feature_behavior=FeatureBehavior( behavior=get_behavior('environmental_constraint')), _categories=[ Category(key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( config_entity, DbEntity( key=Key.WETLAND, feature_class_configuration=FeatureClassConfiguration( abstract_class=SacogWetlandFeature), feature_behavior=FeatureBehavior( behavior=get_behavior('environmental_constraint')), _categories=[ Category(key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( config_entity, DbEntity( key=Key.STREAM, feature_class_configuration=FeatureClassConfiguration( abstract_class=SacogStreamFeature), feature_behavior=FeatureBehavior( behavior=get_behavior('environmental_constraint')), _categories=[ Category(key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), ])
def resolve_key(self, value): resolved_value = getattr(value, self.foreign_key_attribute) if self.is_through else value return get_property_path(resolved_value, resolve_key_property(resolved_value))
def create_query(attribute, config_entity, layer): db_entity = layer.db_entity_interest.db_entity feature_class = config_entity.db_entity_feature_class(db_entity.key) # Create a query that selects the wkb_geometry and the attribute we need # There's nothing to prevent styling multiple attributes in the future try: query = str( feature_class.objects.values( *unique(['wkb_geometry', attribute])).query) except Exception, e: raise Exception( "Error creating the query for db_entity %s. Original exception: %s" % (db_entity.key, e.message)) column_alias = get_property_path( layer.configuration, 'column_alias_lookup.{0}'.format(attribute)) # This would be better done by values() supporting aliases: # https://code.djangoproject.com/attachment/ticket/16735/column_alias.diff # There is a patch available at https://code.djangoproject.com/attachment/ticket/16735/column_alias.diff # that could be applied instead of this: # Replace the select column with the colum as alias. # Only 1 replacement is done to avoid mutilating a join with the same column updated_query = query.replace( '{column_alias}"'.format(column_alias=column_alias), '{column_alias}" as {attribute}'.format(column_alias=column_alias, attribute=attribute), 1) if column_alias else query return updated_query
def default_db_entities(self, **kwargs): """ Project specific SACOG additional db_entities :param default_dict: :return: """ project = self.config_entity # The DbEntity keyspace. These keys have no prefix Key = SacogDbEntityKey # The Behavior keyspace behavior_key = BehaviorKey.Fab.ricate # Used to load Behaviors defined elsewhere get_behavior = lambda key: Behavior.objects.get(key=behavior_key(key)) return super(SacogProjectFixture, self).default_db_entities() + [ update_or_create_db_entity( project, DbEntity( key=DbEntityKey.BASE_CANVAS, # Override. If a name override is supplied, put it in. Otherwise leave null to derive it from the key name='UF Urban Base Canvas', # name=get_property_path(kwargs, 'overrides.%s.name' % Key.BASE), feature_class_configuration=FeatureClassConfiguration( abstract_class=CanvasFeature, # The Base Feature is normally considered a primary_geography unless overridden primary_geography=get_property_path( kwargs, 'overrides.%s.primary_geography' % Key.BASE_CANVAS) or True, primary_key='geography_id', primary_key_type='int', # The Base Feature is normally associated to a subclass of Geography unless overridden geography_class_name=get_property_path( kwargs, 'overrides.%s.geography_class_name' % Key.BASE_CANVAS) or 'footprint.main.models.geographies.parcel.Parcel', # Create a built_form ForeignKey to a single BuiltForm, # by initially joining our 'built_form_key' attribute to its 'key' attribute related_fields=dict(built_form=dict( single=True, related_class_name=uf_model( 'built_form.built_form.BuiltForm'), source_class_join_field_name='built_form_key', related_class_join_field_name='key', ))), feature_behavior=FeatureBehavior( behavior=get_behavior('base_feature'), ), _categories=[ Category(key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( project, DbEntity( key=Key.EXISTING_LAND_USE_PARCELS, feature_class_configuration=FeatureClassConfiguration( abstract_class=SacogExistingLandUseParcelFeature, primary_key='geography_id', primary_key_type='int', fields=dict(), related_fields=dict(land_use_definition=dict( single=True, related_class_name= 'footprint.client.configuration.sacog.built_form.sacog_land_use_definition.SacogLandUseDefinition', resource_model_class_name= 'footprint.main.models.built_form.ClientLandUseDefinition', related_class_join_field_name='land_use', source_class_join_field_name='land_use'))), feature_behavior=FeatureBehavior( behavior=get_behavior('reference'), intersection=AttributeIntersection(from_attribute='id', to_attribute='id')), _categories=[ Category(key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( project, DbEntity( key=DbEntityKey.BASE_AGRICULTURE_CANVAS, name='UF Agriculture Base Canvas (RUCS)', feature_class_configuration=FeatureClassConfiguration( abstract_class=AgricultureFeature, primary_key='geography_id', primary_key_type='varchar', # Create a built_form ForeignKey to a single BuiltForm, # by initially joining our 'built_form_key' attribute to its 'key' attribute related_fields=dict(built_form=dict( single=True, related_class_name= 'footprint.main.models.built_form.built_form.BuiltForm', related_class_join_field_name='key', source_class_join_field_name='built_form_key'))), feature_behavior=FeatureBehavior( behavior=get_behavior('base_agriculture'), intersection=AttributeIntersection( join_type='attribute')), _categories=[ Category(key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( project, DbEntity(key=Key.ELK_GROVE_LAND_USE_PARCELS, feature_class_configuration=FeatureClassConfiguration( abstract_class=ElkGroveLandUseParcel), feature_behavior=FeatureBehavior( behavior=get_behavior('reference'), intersection=GeographicIntersection. centroid_to_polygon), _categories=[ Category( key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( project, DbEntity(key=Key.CENSUS_RATES, feature_class_configuration=FeatureClassConfiguration( abstract_class=CensusRatesFeature), feature_behavior=FeatureBehavior( behavior=get_behavior('reference'), intersection=GeographicIntersection. polygon_to_centroid), _categories=[ Category( key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( project, DbEntity(key=Key.GRID_150M, feature_class_configuration=FeatureClassConfiguration( abstract_class=PhGridFeature), feature_behavior=FeatureBehavior( behavior=get_behavior('internal_analysis'), intersection=GeographicIntersection. polygon_to_centroid), _categories=[ Category( key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( project, DbEntity( key=DbEntityKey.CLIMATE_ZONES, feature_class_configuration=FeatureClassConfiguration( abstract_class=ClimateZoneFeature, related_fields=dict( evapotranspiration_zone=dict( single=True, related_class_name=uf_model( 'policy.water.evapotranspiration_baseline.EvapotranspirationBaseline' ), related_class_join_field_name='zone', source_class_join_field_name= 'evapotranspiration_zone_id'), forecasting_climate_zone=dict( single=True, related_class_name=uf_model( 'policy.energy.commercial_energy_baseline.CommercialEnergyBaseline' ), related_class_join_field_name='zone', source_class_join_field_name= 'forecasting_climate_zone_id'), title_24_zone=dict( single=True, related_class_name=uf_model( 'policy.energy.residential_energy_baseline.ResidentialEnergyBaseline' ), related_class_join_field_name='zone', source_class_join_field_name='title_24_zone_id' ))), feature_behavior=FeatureBehavior( behavior=get_behavior('internal_analysis'), intersection=GeographicIntersection.polygon_to_centroid ), _categories=[ Category(key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( project, DbEntity(key=DbEntityKey.VMT_FUTURE_TRIP_LENGTHS, feature_class_configuration=FeatureClassConfiguration( abstract_class=VmtTripLengthsFeature), feature_behavior=FeatureBehavior( behavior=get_behavior('internal_analysis'), intersection=GeographicIntersection. polygon_to_centroid), _categories=[ Category( key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( project, DbEntity(key=DbEntityKey.VMT_BASE_TRIP_LENGTHS, feature_class_configuration=FeatureClassConfiguration( abstract_class=VmtTripLengthsFeature), feature_behavior=FeatureBehavior( behavior=get_behavior('internal_analysis'), intersection=GeographicIntersection. polygon_to_centroid), _categories=[ Category( key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( project, DbEntity(key=Key.BASE_TRANSIT_STOPS, name='GTFS Transit Stops: 2014', feature_class_configuration=FeatureClassConfiguration( abstract_class=TransitStopFeature), feature_behavior=FeatureBehavior( behavior=get_behavior('internal_analysis'), intersection=GeographicIntersection. polygon_to_polygon), _categories=[ Category( key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), update_or_create_db_entity( project, DbEntity(key=Key.FUTURE_TRANSIT_STOPS, name='GTFS and Future Transit Stops: 2040', feature_class_configuration=FeatureClassConfiguration( abstract_class=TransitStopFeature), feature_behavior=FeatureBehavior( behavior=get_behavior('internal_analysis'), intersection=GeographicIntersection. polygon_to_polygon), _categories=[ Category( key=DbEntityCategoryKey.KEY_CLASSIFICATION, value=DbEntityCategoryKey.REFERENCE) ])), ]
logger.info("Creating layer %s" % layer_key) return TSLayer(key=layer_key, value=raster_layer) def create_query(attribute, config_entity, layer): db_entity = layer.db_entity_interest.db_entity feature_class = config_entity.db_entity_feature_class(db_entity.key) # Create a query that selects the wkb_geometry and the attribute we need # There's nothing to prevent styling multiple attributes in the future try: query = str(feature_class.objects.values(*unique(['wkb_geometry', attribute])).query) except Exception, e: raise Exception("Error creating the query for db_entity %s. Original exception: %s" % (db_entity.key, e.message)) column_alias = get_property_path(layer.configuration, 'column_alias_lookup.{0}'.format(attribute)) # This would be better done by values() supporting aliases: # https://code.djangoproject.com/attachment/ticket/16735/column_alias.diff # There is a patch available at https://code.djangoproject.com/attachment/ticket/16735/column_alias.diff # that could be applied instead of this: # Replace the select column with the colum as alias. # Only 1 replacement is done to avoid mutilating a join with the same column updated_query = query.replace( '{column_alias}"'.format(column_alias=column_alias), '{column_alias}" as {attribute}'.format( column_alias=column_alias, attribute=attribute), 1) if column_alias else query return updated_query