Exemple #1
0
    def create_department_boundary_shapefile(self, queryset, filename, geom):
        path = mkdtemp()
        file_path = os.path.join(path, filename)
        geom_type = ogr.wkbPoint

        if queryset.model._meta.get_field_by_name(
                geom)[0].geom_type == 'MULTIPOLYGON':
            geom_type = ogr.wkbMultiPolygon

        driver = ogr.GetDriverByName("ESRI Shapefile")

        # create the data source
        data_source = driver.CreateDataSource(file_path)

        # create the spatial reference, WGS84
        srs = osr.SpatialReference()
        srs.ImportFromEPSG(4326)

        # create the layer
        layer = data_source.CreateLayer(filename, srs, geom_type)
        for row in queryset:
            raw_geom = getattr(row, geom)
            wkt = None

            if raw_geom:
                wkt = raw_geom.wkt
            else:
                continue

            feature = ogr.Feature(layer.GetLayerDefn())
            point = ogr.CreateGeometryFromWkt(wkt)

            # Set the feature geometry using the point
            feature.SetGeometry(point)
            layer.CreateFeature(feature)
            feature.Destroy()

        data_source.Destroy()

        if os.path.exists(os.path.join(self.output_dir, filename + '.zip')):
            filename += '-' + str(uuid.uuid4())[:5]

        zip_file = shutil.make_archive(self.output_dir + '/' + filename, 'zip',
                                       file_path)

        if self.request.META.get('SERVER_NAME') != 'testserver':
            remove_file.delay(path)
            remove_file.delay(zip_file, countdown=600)

        return file_path, zip_file
Exemple #2
0
    def create_department_boundary_shapefile(self, queryset, filename, geom):
        path = mkdtemp()
        file_path = os.path.join(path, filename)
        geom_type = ogr.wkbPoint

        if queryset.model._meta.get_field_by_name(geom)[0].geom_type == 'MULTIPOLYGON':
            geom_type = ogr.wkbMultiPolygon

        driver = ogr.GetDriverByName("ESRI Shapefile")

        # create the data source
        data_source = driver.CreateDataSource(file_path)

        # create the spatial reference, WGS84
        srs = osr.SpatialReference()
        srs.ImportFromEPSG(4326)

        # create the layer
        layer = data_source.CreateLayer(filename, srs, geom_type)
        for row in queryset:
            raw_geom = getattr(row, geom)
            wkt = None

            if raw_geom:
                wkt = raw_geom.wkt
            else:
                continue

            feature = ogr.Feature(layer.GetLayerDefn())
            point = ogr.CreateGeometryFromWkt(wkt)

            # Set the feature geometry using the point
            feature.SetGeometry(point)
            layer.CreateFeature(feature)
            feature.Destroy()

        data_source.Destroy()

        if os.path.exists(os.path.join(self.output_dir, filename + '.zip')):
            filename += '-' + str(uuid.uuid4())[:5]

        zip_file = shutil.make_archive(self.output_dir + '/' + filename, 'zip', file_path)

        if self.request.META.get('SERVER_NAME') != 'testserver':
            remove_file.delay(path)
            remove_file.delay(zip_file, countdown=600)

        return file_path, zip_file
Exemple #3
0
    def create_shapefile(self, queryset, filename, geom):
        path = mkdtemp()
        file_path = os.path.join(path, filename)
        geom_type = ogr.wkbPoint

        if queryset.model._meta.get_field_by_name(
                geom)[0].geom_type == 'MULTIPOLYGON':
            geom_type = ogr.wkbMultiPolygon

        driver = ogr.GetDriverByName("ESRI Shapefile")

        # create the data source
        data_source = driver.CreateDataSource(file_path)

        # create the spatial reference, WGS84
        srs = osr.SpatialReference()
        srs.ImportFromEPSG(4326)

        # create the layer
        layer = data_source.CreateLayer(filename, srs, geom_type)

        # Name, Type, Width
        fields = [
            ('id', ogr.OFTInteger, None),
            ('name', ogr.OFTString, 254),
            ('department', ogr.OFTInteger, None),
            ('station_nu', ogr.OFTInteger, None),
            ('address_l1', ogr.OFTString, 100),
            ('address_l2', ogr.OFTString, 100),
            ('city', ogr.OFTString, 50),
            ('state', ogr.OFTString, 40),
            ('zipcode', ogr.OFTString, 10),
            ('country', ogr.OFTString, 2),
        ]

        ids = queryset.values_list('id', flat=True)

        # Pivot staffing rows per apparatus without aggregating
        # resulting in multiple columns if a station has more than one of any apparatus type
        for apparatus, apparatus_alias in Staffing.APPARATUS_SHAPEFILE_CHOICES:

            cursor = connection.cursor()
            sql = "select count(*) from firestation_staffing  where firestation_id in %s and apparatus=%s " \
                  "group by firestation_id order by count(*) desc limit 1;"
            cursor.execute(sql, [tuple(ids), apparatus])
            row = cursor.fetchone()
            count = 0

            if row:
                count = row[0]

            for n in range(0, count) or [0]:
                field_name = apparatus_alias

                if n > 0:
                    field_name = apparatus_alias + '_{0}'.format(n)

                fields.append((field_name, ogr.OFTInteger, None))

        for alias, field_type, width in fields:
            field = ogr.FieldDefn(alias, field_type)

            if width:
                field.SetWidth(width)

            layer.CreateField(field)
            field = None

        # Process the text file and add the attributes and features to the shapefile
        for row in queryset:

            raw_geom = getattr(row, geom)
            wkt = None

            if raw_geom:
                wkt = raw_geom.wkt
            else:
                continue

            feature = ogr.Feature(layer.GetLayerDefn())

            # Set the attributes using the values from the delimited text file
            feature.SetField('id', row.id)
            feature.SetField('name', str(row.name))
            feature.SetField('department', row.department.id)
            feature.SetField('station_nu', row.station_number)

            feature.SetField(
                'address_l1',
                str(getattr(row.station_address, 'address_line1', str))
                or None)
            feature.SetField(
                'address_l2',
                str(getattr(row.station_address, 'address_line2', str))
                or None)
            feature.SetField(
                'city',
                str(getattr(row.station_address, 'city', str)) or None)
            feature.SetField(
                'state',
                str(getattr(row.station_address, 'state_province', str))
                or None)
            feature.SetField(
                'zipcode',
                str(getattr(row.station_address, 'postal_code', str)) or None)
            feature.SetField(
                'country',
                str(getattr(row.station_address, 'country_id', str)) or None)

            # Populate staffing for each unit
            for apparatus, apparatus_alias in Staffing.APPARATUS_SHAPEFILE_CHOICES:

                for n, record in enumerate(
                        row.staffing_set.filter(apparatus=apparatus)):
                    apparatus_alias_index = apparatus_alias

                    if n > 0:
                        apparatus_alias_index += '_{0}'.format(n)

                    feature.SetField(apparatus_alias_index, record.personnel)

            # Create the point from the Well Known Txt
            point = ogr.CreateGeometryFromWkt(wkt)

            # Set the feature geometry using the point
            feature.SetGeometry(point)
            # Create the feature in the layer (shapefile)
            layer.CreateFeature(feature)
            # Destroy the feature to free resources
            feature.Destroy()

        # Destroy the data source to free resources
        data_source.Destroy()

        if os.path.exists(os.path.join(self.output_dir, filename + '.zip')):
            filename += '-' + str(uuid.uuid4())[:5]

        zip_file = shutil.make_archive(self.output_dir + '/' + filename, 'zip',
                                       file_path)

        if self.request.META.get('SERVER_NAME') != 'testserver':
            remove_file.delay(path)
            remove_file.delay(zip_file, countdown=600)

        return file_path, zip_file
Exemple #4
0
    def create_shapefile(self, queryset, filename, geom):
        path = mkdtemp()
        file_path = os.path.join(path, filename)
        geom_type = ogr.wkbPoint

        if queryset.model._meta.get_field_by_name(geom)[0].geom_type == 'MULTIPOLYGON':
            geom_type = ogr.wkbMultiPolygon

        driver = ogr.GetDriverByName("ESRI Shapefile")

        # create the data source
        data_source = driver.CreateDataSource(file_path)

        # create the spatial reference, WGS84
        srs = osr.SpatialReference()
        srs.ImportFromEPSG(4326)

        # create the layer
        layer = data_source.CreateLayer(filename, srs, geom_type)

        # Name, Type, Width
        fields = [
            ('id', ogr.OFTInteger, None),
            ('name', ogr.OFTString, 254),
            ('department', ogr.OFTInteger, None),
            ('station_nu', ogr.OFTInteger, None),
            ('address_l1', ogr.OFTString, 100),
            ('address_l2', ogr.OFTString, 100),
            ('city', ogr.OFTString, 50),
            ('state', ogr.OFTString, 40),
            ('zipcode', ogr.OFTString, 10),
            ('country', ogr.OFTString, 2),
        ]

        ids = queryset.values_list('id', flat=True)

        # Pivot staffing rows per apparatus without aggregating
        # resulting in multiple columns if a station has more than one of any apparatus type
        for apparatus, apparatus_alias in Staffing.APPARATUS_SHAPEFILE_CHOICES:

            cursor = connection.cursor()
            sql = "select count(*) from firestation_staffing  where firestation_id in %s and apparatus=%s " \
                  "group by firestation_id order by count(*) desc limit 1;"
            cursor.execute(sql, [tuple(ids), apparatus])
            row = cursor.fetchone()
            count = 0

            if row:
                count = row[0]

            for n in range(0, count) or [0]:
                field_name = apparatus_alias

                if n > 0:
                    field_name = apparatus_alias + '_{0}'.format(n)

                fields.append((field_name, ogr.OFTInteger, None))

        for alias, field_type, width in fields:
            field = ogr.FieldDefn(alias, field_type)

            if width:
                field.SetWidth(width)

            layer.CreateField(field)
            field = None

        # Process the text file and add the attributes and features to the shapefile
        for row in queryset:

            raw_geom = getattr(row, geom)
            wkt = None

            if raw_geom:
              wkt = raw_geom.wkt

            else:
                continue

            feature = ogr.Feature(layer.GetLayerDefn())

            # Set the attributes using the values from the delimited text file
            feature.SetField('id', row.id)
            feature.SetField('name', str(row.name))
            feature.SetField('department', row.department.id)
            feature.SetField('station_nu', row.station_number)

            feature.SetField('address_l1', str(getattr(row.station_address, 'address_line1', str)) or None)
            feature.SetField('address_l2', str(getattr(row.station_address, 'address_line2', str)) or None)
            feature.SetField('city', str(getattr(row.station_address, 'city', str)) or None)
            feature.SetField('state', str(getattr(row.station_address, 'state_province', str)) or None)
            feature.SetField('zipcode', str(getattr(row.station_address, 'postal_code', str)) or None)
            feature.SetField('country', str(getattr(row.station_address, 'country_id', str)) or None)

            # Populate staffing for each unit
            for apparatus, apparatus_alias in Staffing.APPARATUS_SHAPEFILE_CHOICES:

                for n, record in enumerate(row.staffing_set.filter(apparatus=apparatus)):
                    apparatus_alias_index = apparatus_alias

                    if n > 0:
                        apparatus_alias_index += '_{0}'.format(n)

                    feature.SetField(apparatus_alias_index, record.personnel)


            # Create the point from the Well Known Txt
            point = ogr.CreateGeometryFromWkt(wkt)

            # Set the feature geometry using the point
            feature.SetGeometry(point)
            # Create the feature in the layer (shapefile)
            layer.CreateFeature(feature)
            # Destroy the feature to free resources
            feature.Destroy()

        # Destroy the data source to free resources
        data_source.Destroy()

        if os.path.exists(os.path.join(self.output_dir, filename + '.zip')):
            filename += '-' + str(uuid.uuid4())[:5]

        zip_file = shutil.make_archive(self.output_dir + '/' + filename, 'zip', file_path)

        if self.request.META.get('SERVER_NAME') != 'testserver':
            remove_file.delay(path)
            remove_file.delay(zip_file, countdown=600)

        return file_path, zip_file