def add_boundaries_for_layer(self, config, layer, bset, database):
        # Get spatial reference system for the postgis geometry field
        geometry_field = Boundary._meta.get_field_by_name(GEOMETRY_COLUMN)[0]
        SpatialRefSys = connections[database].ops.spatial_ref_sys()
        db_srs = SpatialRefSys.objects.using(database).get(
            srid=geometry_field.srid).srs

        if 'srid' in config and config['srid']:
            layer_srs = SpatialRefSys.objects.get(srid=config['srid']).srs
        else:
            layer_srs = layer.srs

        # Simplification can be configured but default is to create simplified
        # geometry field by collapsing points within 1/1000th of a degree.
        # For reference, Chicago is at approx. 42 degrees latitude this works
        # out to a margin of roughly 80 meters latitude and 112 meters
        # longitude for Chicago area.
        simplification = config.get('simplification', 0.0001)

        # Create a convertor to turn the source data into
        transformer = CoordTransform(layer_srs, db_srs)

        for feature in layer:
            log.debug("Processing boundary %s" % feature)
            # Transform the geometry to the correct SRS
            geometry = self.polygon_to_multipolygon(feature.geom)
            geometry.transform(transformer)

            # Preserve topology prevents a shape from ever crossing over
            # itself.
            simple_geometry = geometry.geos.simplify(simplification,
                                                     preserve_topology=True)

            # Conversion may force multipolygons back to being polygons
            simple_geometry = self.polygon_to_multipolygon(simple_geometry.ogr)

            # Extract metadata into a dictionary
            metadata = {}

            for field in layer.fields:

                # Decode string fields using encoding specified in definitions
                # config
                if config['encoding'] != '':
                    try:
                        metadata[field] = feature.get(field).decode(
                            config['encoding'])
                    # Only strings will be decoded, get value in normal way if
                    # int etc.
                    except __HOLE__:
                        metadata[field] = feature.get(field)
                else:
                    metadata[field] = feature.get(field)

            external_id = config['ider'](feature)
            feature_name = config['namer'](feature)

            # If encoding is specified, decode id and feature name
            if config['encoding'] != '':
                external_id = external_id.decode(config['encoding'])
                feature_name = feature_name.decode(config['encoding'])

            if config['kind_first']:
                display_name = '%s %s' % (config['singular'], feature_name)
            else:
                display_name = '%s %s' % (feature_name, config['singular'])

            Boundary.objects.create(
                set=bset,
                kind=config['singular'],
                external_id=external_id,
                name=feature_name,
                display_name=display_name,
                metadata=metadata,
                shape=geometry.wkt,
                simple_shape=simple_geometry.wkt,
                centroid=geometry.geos.centroid)
dataset/ETHPy150Open newsapps/django-boundaryservice/boundaryservice/management/commands/loadshapefiles.py/Command.add_boundaries_for_layer