コード例 #1
0
def create_areas(layer):
    """Create area for each item in layer."""
    ct = CoordTransform(SpatialReference(RD), SpatialReference(WGS84))
    for feat in layer:
        dataset = retrieve_dataset(feat)
        if dataset is None:
            continue
        logger.debug(dataset.name)

        feat_geometry = feat.geom
        feat_geometry.transform(ct)

        geom = GEOSGeometry(feat_geometry.wkb)
	if Area.objects.filter(ident=feat.get(FIELDS_MAPPING['ident'])).exists():
            continue
        area = Area(geometry=geom,
                    geo_object_group=geo_object_group(),
                    ident=feat.get(FIELDS_MAPPING['ident']),
                    name=feat.get(FIELDS_MAPPING['name']),
                    areasort_krw=feat.get(FIELDS_MAPPING['areasort_krw']),
		    area_class=Area.AREA_CLASS_KRW_WATERLICHAAM,
		    is_active=True,
                    data_set=dataset)
        area.save()

	# create waterbody
	WaterBody(area=area).save()
	
        logger.debug('Object {0} saved'.format(feat.get(FIELDS_MAPPING['ident'])))
コード例 #2
0
def import_waterbodies(wb_import_settings):
    """
    Create geoobjects and waterbodies.
    """
    owmsources = wb_import_settings['owm_sources']

    # Reset geo object groups
    geogroups = {}
    category = Category.objects.get(slug='krw-waterlichamen')

    for s in wb_import_settings['owm_sources']:
        # Determine geoobject group name
        geo_object_group_name = ('measure::waterbody::%s' %
                                 os.path.basename(s['file']))

        # Remove geoobject group by name if it exists
        try:
            print 'Finding existing geoobject group named %s' % (
                geo_object_group_name,
            )
            geo_object_group = GeoObjectGroup.objects.get(
                name=geo_object_group_name)
            print 'Deleting existing geoobject group named %s' % (
                geo_object_group_name,
            )
            geo_object_group.delete()
        except GeoObjectGroup.DoesNotExist:
            pass

        # Create new geoobject group with that name
        geo_object_group = GeoObjectGroup(
            name=geo_object_group_name,
            slug=slugify(os.path.basename(s['file']).split('.')[-2]),
            created_by=s['user'],
        )
        geo_object_group.save()

        # Add this geoobject group to lizard_area category of waterbodies
        category.geo_object_groups.add(geo_object_group)

        # Keep a dict of geo_object_groups for later assignment
        geogroups[s['data_administrator'].name] = geo_object_group

    # Make a mapping of owmidents to sets of owaidents
    # There are multiple owaidents per owmident
    owa_idents = {}
    for rec in _records(wb_import_settings['link_file']):
        owm = rec['owmident']
        owa = rec['owaident']
        if owm not in owa_idents:
            owa_idents[owm] = []
        owa_idents[owm].append(owa)

    # Loop geofile, create geometry object for each geometry
    owa_geometry = {}
    spatialreference_wgs84 = SpatialReference(WGS84)
    spatialreference_rd = SpatialReference(RD)
    coordtransform = CoordTransform(
        spatialreference_rd,
        spatialreference_wgs84,
    )
    for f in wb_import_settings['geometry_files']:
        for rec in _records(f):
            geometry = GEOSGeometry(
                rec['wkb_geometry'],
                #srid=28992,
            )
            geometry.transform(coordtransform)
            owa_geometry[rec['owaident']] = geometry

    # Go through the owm files, to get or create the corresponding areas
    for s in owmsources:
        for rec in _records(s['file']):
            owm_ident = rec['owmident'].strip()
            # Get or create area
            try:
                area = Area.objects.get(ident=owm_ident)
            except Area.DoesNotExist:
                owa_geometries = [owa_geometry[owa_ident]
                                  for owa_ident in owa_idents[owm_ident]]
                area = Area(
                    # Fields from GeoObject
                    ident=owm_ident,
                    geometry=_combine(owa_geometries),
                    geo_object_group=geogroups[s['data_administrator'].name],
                    # Fields from Area
                    area_class=Area.AREA_CLASS_KRW_WATERLICHAAM,
                    data_administrator=s['data_administrator'],
                    parent=None,
                    # Fields from Communique
                    name=rec['owmnaam'].strip(),
                    code=None,
                    description='',
                )
            # Set data_set on area in any case, existing or imported here.
            area.data_set = s['data_set']
            area.save()

            # Create WaterBody
            krw_status = KRWStatus.objects.get(code=rec['owmstat'].strip())
            krw_watertype = KRWWatertype.objects.get(
                code=rec['owmtype'].strip()
            )
            waterbody, waterbody_created = _get_or_create(
                model=WaterBody,
                get_kwargs={'area_ident': area.ident},
                extra_kwargs={
                    'area': area,
                    'krw_status': krw_status,
                    'krw_watertype': krw_watertype,
                },
            )
コード例 #3
0
 def Area(self):
     area = Area()
     area.save = lambda a=area: self.areas.append(a)
     return area
コード例 #4
0
class StructureTest(TestCase):

    def setUp(self):
        self.area = None
        self.area_configuration = None
        self.create_area()
        self.create_areaconfiguration(self.area)
        self.create_structure_in_out()

    def create_area(self):
        user = User(username='******', password='******')
        user.save()
        geo_object_group = self.get_or_create_geoobjectgroup(
            user.username)
        self.area = Area(ident="test", name="test",
                         geo_object_group=geo_object_group,
                         geometry=GEOSGeometry(Point(0, 0), srid=4326),
                         data_administrator_id=1)
        self.area.save()

    def create_structure_in_out(self):
        """Add in/out objects into StructureInOut."""
        StructureInOut(code='in', index=1, description="In default").save()
        StructureInOut(code='uit', index=0, description="Uit default").save()

    def create_areaconfiguration(self, area):
        try:
            self.area_configuration = AreaConfiguration(
                ident=area.ident,
                name=area.name,
                area=area)
            self.area_configuration.save()
            return True
        except:
            return False

    def test_create_structures(self):
        """Test creating of 10 structures."""
        self.area_configuration.create_default_structures()
        structures = Structure.objects.all()
        self.assertEquals(len(structures), 10)

        self.area_configuration.create_default_structures()
        structures = Structure.objects.all()
        self.assertEquals(len(structures), 10)

    def get_or_create_geoobjectgroup(self, user_name):
        from lizard_geo.models import GeoObjectGroup
        user_obj = User.objects.get(username=user_name)
        group_name = 'test'
        group_slug = 'test'
        geo_object_group, created = GeoObjectGroup.objects.get_or_create(
            name=group_name, slug=group_slug, created_by=user_obj)
        if created:
            geo_object_group.source_log = 'test'
            geo_object_group.save()
        return geo_object_group

    def tearDown(self):
        """Clear the Stuctures and AreaConfiguration."""
        if self.area is not None:
            Area.objects.all().delete
        if self.area_configuration is not None:
            AreaConfiguration.objects.all().delete()
コード例 #5
0
ファイル: geoobject.py プロジェクト: pombredanne/lizard-area
def import_shapefile_area(shapefile_filename, srid, user, data_administrator, file_type):
    """
    Load shapefile with communique and put it in Communique(GeoObject)
    and GeoObjectGroup

    From: import_geoobject_shapefile in rainapp
    """
    # shapefile_filename = resource_filename('lizard_rainapp',
    #                                        'shape/gemeenten2009.shp')

    original_srs = ogr.osr.SpatialReference()
    original_srs.ImportFromEPSG(int(srid))
    target_srs = ogr.osr.SpatialReference()
    target_srs.ImportFromEPSG(4326)
    coordinate_transformation = ogr.osr.CoordinateTransformation(original_srs, target_srs)

    drv = ogr.GetDriverByName("ESRI Shapefile")
    source = drv.Open(shapefile_filename)
    layer = source.GetLayer()

    logger.info("Importing new geoobjects from %s...", shapefile_filename)
    number_of_features = 0

    # Create group with geo objects.
    geo_object_group = GeoObjectGroup(
        name=shapefile_filename[:128], slug=slugify(shapefile_filename[:128]), created_by=user
    )
    geo_object_group.save()

    done = {}  # Check for duplicates

    # Add all geo objects to group.
    for feature in layer:
        geom = feature.GetGeometryRef()
        # Optional extra things to do with the shape
        geom.Transform(coordinate_transformation)
        geom.FlattenTo2D()
        # import pdb;pdb.set_trace()

        # # KRW Waterlichamen merge
        # kwargs = {
        #     'ident': feature.GetField(feature.GetFieldIndex('OWAIDENT')),
        #     'geometry': GEOSGeometry(geom.ExportToWkt(), srid=4326),
        #     'geo_object_group': geo_object_group,

        #     # Communique
        #     'name': '%s' % feature.GetField(
        #         feature.GetFieldIndex('OWANAAM')),

        #     # Area
        #     'data_administrator': data_administrator,
        #     'area_class': Area.AREA_CLASS_KRW_WATERLICHAAM,
        # }

        # # Afvoergebieden, 2 varianten
        # # area_code, _ = AreaCode.objects.get_or_create(
        # #     name=feature.GetField(feature.GetFieldIndex('GAFCODE')))
        # kwargs = {
        #     'ident': feature.GetField(feature.GetFieldIndex('GAFIDENT')),
        #     'geometry': GEOSGeometry(geom.ExportToWkt(), srid=4326),
        #     'geo_object_group': geo_object_group,

        #     # Communique
        #     'name': unicode(
        #         feature.GetField(feature.GetFieldIndex('GAFNAAM')),
        #         errors='ignore'),
        #     #'code': area_code,
        #     # Area
        #     'data_administrator': data_administrator,
        #     'area_class': Area.AREA_CLASS_AAN_AFVOERGEBIED,
        # }

        # Deel afvoergebieden: watervlakken_mbp_sap
        # area_code, _ = AreaCode.objects.get_or_create(
        #     name=feature.GetField(feature.GetFieldIndex('GAFCODE')))
        parent = Area.objects.get(name=unicode(feature.GetField(feature.GetFieldIndex("GAFNAAM")), errors="ignore"))
        # Only field that is unique.
        name = "Deelgebied %s" % feature.GetField(feature.GetFieldIndex("FID_GBKNwa"))
        ident = ("LSDR-%s" % feature.GetField(feature.GetFieldIndex("FID_GBKNwa")),)  # Else it is not unique
        kwargs = {
            "ident": ident,
            "geometry": GEOSGeometry(geom.ExportToWkt(), srid=4326),
            "geo_object_group": geo_object_group,
            # Communique
            "name": name,
            #'code': area_code,
            # Area
            "data_administrator": data_administrator,
            "area_class": Area.AREA_CLASS_DEEL_AAN_AFVOERGEBIED,
            "parent": parent,
        }

        if ident not in done:
            geo_object = Area(**kwargs)
            geo_object.save()
            done[ident] = None
        else:
            logger.warning("Ignored: Ident %s is already imported in this session" % ident)
        number_of_features += 1

    logger.info("Added %s with %s geo objects.", shapefile_filename, number_of_features)