Ejemplo n.º 1
0
def shape_write(iterable, model, columns, get_geom, geom_type, srid, srid_out=None):
    """
    Write tempfile with shape layer.
    """

    headers = []
    columns_headers = {}
    for field in columns:
        c = getattr(model, "%s_verbose_name" % field, None)
        if c is None:
            try:
                c = model._meta.get_field(field).verbose_name
            except FieldDoesNotExist:
                c = _(field.title())

        reponse = unicode(c)
        reponse = unicodedata.normalize("NFD", reponse)
        reponse = smart_str(reponse.encode("ascii", "ignore")).replace(" ", "_").lower()

        headers.append(reponse)
        columns_headers[field] = reponse

    tmp_file, layer, ds, native_srs, output_srs, column_map = create_shape_format_layer(
        headers, geom_type, srid, srid_out
    )

    feature_def = layer.GetLayerDefn()

    if native_srs != output_srs:
        ct = osr.CoordinateTransformation(native_srs, output_srs)

        def transform(ogr_geom):
            ogr_geom.Transform(ct)
            return ogr_geom

    else:

        def transform(ogr_geom):
            return ogr_geom

    for item in iterable:
        feat = ogr.Feature(feature_def)

        for fieldname in columns:
            # They are all String (see create_shape_format_layer)
            value = field_as_string(item, fieldname, ascii=True)

            feat.SetField(column_map.get(columns_headers.get(fieldname)), value[:254])

        geom = get_geom(item)
        if geom:
            ogr_geom = transform(ogr.CreateGeometryFromWkt(geom.wkt))
            check_err(feat.SetGeometry(ogr_geom))

        check_err(layer.CreateFeature(feat))

    ds.Destroy()

    return tmp_file.name
Ejemplo n.º 2
0
 def write_with_native_bindings(self,tmp_name,queryset,geo_field):
     """ Write a shapefile out to a file from a geoqueryset.
     
     Written by Jared Kibele and Dane Springmeyer.
     
     In this case we use the python bindings available with a build
     of gdal when compiled with --with-python, instead of the ctypes-based 
     bindings that GeoDjango provides.
     
     """
     dr = ogr.GetDriverByName('ESRI Shapefile')
     ds = dr.CreateDataSource(tmp_name)
     if ds is None:
         raise Exception('Could not create file!')
     
     if hasattr(geo_field,'geom_type'):
         ogr_type = OGRGeomType(geo_field.geom_type).num
     else:
         ogr_type = OGRGeomType(geo_field._geom).num
     
     native_srs = osr.SpatialReference()
     if hasattr(geo_field,'srid'):
         native_srs.ImportFromEPSG(geo_field.srid)
     else:
         native_srs.ImportFromEPSG(geo_field._srid)
     
     layer = ds.CreateLayer('lyr', srs=native_srs, geom_type=ogr_type)
     
     # Get the standard shape attributes
     attributes = self.get_attributes()
     
     # Create columns for the standard shape attributes
     [self.create_field(layer, field.name) for field in attributes]
     
     # Create columnes for all the possible metadata attributes
     [self.create_field(layer, key) for key in self.get_metadata_attr()]
     
     feature_def = layer.GetLayerDefn()
     
     for item in queryset:
         feat = ogr.Feature( feature_def )
         
         for field in attributes:
             value = getattr(item, field.name)
             self.set_field(feat, field.name, value)
         
         for key, value in item.metadata.items():
             self.set_field(feat, key, value)
         
         geom = getattr(item, geo_field.name)
         if geom:
             ogr_geom = ogr.CreateGeometryFromWkt(geom.wkt)
             check_err(feat.SetGeometry(ogr_geom))
         else:
             pass
         
         check_err(layer.CreateFeature(feat))
     
     ds.Destroy()
Ejemplo n.º 3
0
def shape_write(iterable, model, columns, get_geom, geom_type, srid, srid_out=None):
    """
    Write tempfile with shape layer.
    """

    headers = []
    columns_headers = {}
    for field in columns:
        c = getattr(model, '%s_verbose_name' % field, None)
        if c is None:
            try:
                c = model._meta.get_field(field).verbose_name
            except FieldDoesNotExist:
                c = _(field.title())

        reponse = unicode(c)
        reponse = unicodedata.normalize('NFD', reponse)
        reponse = smart_str(reponse.encode('ascii', 'ignore')).replace(' ', '_').lower()

        headers.append(reponse)
        columns_headers[field] = reponse

    tmp_file, layer, ds, native_srs, output_srs, column_map = create_shape_format_layer(headers, geom_type, srid, srid_out)

    feature_def = layer.GetLayerDefn()

    if native_srs != output_srs:
        ct = osr.CoordinateTransformation(native_srs, output_srs)

        def transform(ogr_geom):
            ogr_geom.Transform(ct)
            return ogr_geom
    else:
        def transform(ogr_geom):
            return ogr_geom

    for item in iterable:
        feat = ogr.Feature(feature_def)

        for fieldname in columns:
            # They are all String (see create_shape_format_layer)
            value = field_as_string(item, fieldname, ascii=True)

            feat.SetField(column_map.get(columns_headers.get(fieldname)),
                          value[:254])

        geom = get_geom(item)
        if geom:
            ogr_geom = transform(ogr.CreateGeometryFromWkt(geom.wkt))
            check_err(feat.SetGeometry(ogr_geom))

        check_err(layer.CreateFeature(feat))

    ds.Destroy()

    return tmp_file.name
Ejemplo n.º 4
0
    def add_aois_to_shapefile(self, ds, job_object):
        aois = job_object.aois.all()
        if len(aois) == 0:
            return

        geo_field = aois[0].polygon

        # Get the right geometry type number for ogr
        ogr_type = OGRGeomType(geo_field.geom_type).num

        # Set up the native spatial reference of the geometry field using the srid
        native_srs = SpatialReference(geo_field.srid)

        # create the AOI layer
        layer = lgdal.OGR_DS_CreateLayer(ds, 'lyr', native_srs._ptr, ogr_type,
                                         None)

        # Create the fields that each feature will have
        fields = AOI._meta.fields
        attributes = []
        for field in fields:
            if field.name in 'id, active, name, created_at, updated_at, analyst, priority, status, properties':
                attributes.append(field)

        for field in attributes:
            data_type = 4
            if field.name == 'id':
                data_type = 0
            fld = lgdal.OGR_Fld_Create(str(field.name), data_type)
            added = lgdal.OGR_L_CreateField(layer, fld, 0)
            check_err(added)

        # Getting the Layer feature definition.
        feature_def = lgdal.OGR_L_GetLayerDefn(layer)

        # Loop through queryset creating features
        for item in aois:
            feat = lgdal.OGR_F_Create(feature_def)

            for idx, field in enumerate(attributes):
                if field.name == 'properties':
                    value = json.dumps(item.properties)
                else:
                    value = getattr(item, field.name)
                string_value = str(value)[:80]
                lgdal.OGR_F_SetFieldString(feat, idx, string_value)

            # Transforming & setting the geometry
            geom = item.polygon
            ogr_geom = OGRGeometry(geom.wkt, native_srs)
            check_err(lgdal.OGR_F_SetGeometry(feat, ogr_geom._ptr))

            # create the feature in the layer.
            check_err(lgdal.OGR_L_CreateFeature(layer, feat))

        check_err(lgdal.OGR_L_SyncToDisk(layer))
Ejemplo n.º 5
0
    def add_aois_to_shapefile(self, ds, job_object):
        aois = job_object.aois.all()
        if len(aois) == 0:
            return

        geo_field = aois[0].polygon

        # Get the right geometry type number for ogr
        ogr_type = OGRGeomType(geo_field.geom_type).num

        # Set up the native spatial reference of the geometry field using the srid
        native_srs = SpatialReference(geo_field.srid)

        # create the AOI layer
        layer = lgdal.OGR_DS_CreateLayer(ds, 'Workcells', native_srs._ptr, ogr_type, None)

        # Create the fields that each feature will have
        fields = AOI._meta.fields
        attributes = []
        for field in fields:
            if field.name in 'id, active, name, created_at, updated_at, analyst, priority, status, properties':
                attributes.append(field)

        for field in attributes:
            data_type = 4
            if field.name == 'id':
                data_type = 0
            fld = lgdal.OGR_Fld_Create(str(field.name), data_type)
            added = lgdal.OGR_L_CreateField(layer, fld, 0)
            check_err(added)

        # Getting the Layer feature definition.
        feature_def = lgdal.OGR_L_GetLayerDefn(layer)

        # Loop through queryset creating features
        for item in aois:
            feat = lgdal.OGR_F_Create(feature_def)

            for idx, field in enumerate(attributes):
                if field.name == 'properties':
                    value = json.dumps(item.properties)
                else:
                    value = getattr(item, field.name)
                string_value = str(value)[:80]
                lgdal.OGR_F_SetFieldString(feat, idx, string_value)

            # Transforming & setting the geometry
            geom = item.polygon
            ogr_geom = OGRGeometry(geom.wkt, native_srs)
            check_err(lgdal.OGR_F_SetGeometry(feat, ogr_geom._ptr))

            # create the feature in the layer.
            check_err(lgdal.OGR_L_CreateFeature(layer, feat))

        check_err(lgdal.OGR_L_SyncToDisk(layer))
Ejemplo n.º 6
0
    def add_features_subset_to_shapefile(self, ds, features, layer_name):
        if len(features) == 0:
            return

        geo_field = features[0].the_geom

        # Get the right geometry type number for ogr
        ogr_type = OGRGeomType(geo_field.geom_type).num

        # Set up the native spatial reference of the geometry field using the srid
        native_srs = SpatialReference(geo_field.srid)

        # create the Feature layer
        layer = lgdal.OGR_DS_CreateLayer(ds, layer_name, native_srs._ptr,
                                         ogr_type, None)

        # Create the fields that each feature will have
        fields = Feature._meta.fields
        attributes = []
        for field in fields:
            if field.name in 'id, analyst, template, created_at, updated_at, job, project':
                attributes.append(field)

        for field in attributes:
            data_type = 4
            if field.name == 'id':
                data_type = 0
            fld = lgdal.OGR_Fld_Create(str(field.name), data_type)
            added = lgdal.OGR_L_CreateField(layer, fld, 0)
            check_err(added)

        # Getting the Layer feature definition.
        feature_def = lgdal.OGR_L_GetLayerDefn(layer)

        # Loop through queryset creating features
        for item in features:
            feat = lgdal.OGR_F_Create(feature_def)

            for idx, field in enumerate(attributes):
                value = getattr(item, field.name)
                # if field.name == 'template':
                #     value = value.name
                string_value = str(value)
                lgdal.OGR_F_SetFieldString(feat, idx, string_value)

            # Transforming & setting the geometry
            geom = item.the_geom
            ogr_geom = OGRGeometry(geom.wkt, native_srs)
            check_err(lgdal.OGR_F_SetGeometry(feat, ogr_geom._ptr))

            # create the feature in the layer.
            check_err(lgdal.OGR_L_CreateFeature(layer, feat))

        check_err(lgdal.OGR_L_SyncToDisk(layer))
Ejemplo n.º 7
0
    def add_features_subset_to_shapefile(self, ds, features, layer_name):
        if len(features) == 0:
            return

        geo_field = features[0].the_geom

        # Get the right geometry type number for ogr
        ogr_type = OGRGeomType(geo_field.geom_type).num

        # Set up the native spatial reference of the geometry field using the srid
        native_srs = SpatialReference(geo_field.srid)

        # create the Feature layer
        layer = lgdal.OGR_DS_CreateLayer(ds, layer_name, native_srs._ptr, ogr_type, None)

        # Create the fields that each feature will have
        fields = Feature._meta.fields
        attributes = []
        for field in fields:
            if field.name in 'id, analyst, template, created_at, updated_at, job, project':
                attributes.append(field)

        for field in attributes:
            data_type = 4
            if field.name == 'id':
                data_type = 0
            fld = lgdal.OGR_Fld_Create(str(field.name), data_type)
            added = lgdal.OGR_L_CreateField(layer, fld, 0)
            check_err(added)

        # Getting the Layer feature definition.
        feature_def = lgdal.OGR_L_GetLayerDefn(layer)

        # Loop through queryset creating features
        for item in features:
            feat = lgdal.OGR_F_Create(feature_def)

            for idx, field in enumerate(attributes):
                value = getattr(item, field.name)
                # if field.name == 'template':
                #     value = value.name
                string_value = str(value)
                lgdal.OGR_F_SetFieldString(feat, idx, string_value)

            # Transforming & setting the geometry
            geom = item.the_geom
            ogr_geom = OGRGeometry(geom.wkt, native_srs)
            check_err(lgdal.OGR_F_SetGeometry(feat, ogr_geom._ptr))

            # create the feature in the layer.
            check_err(lgdal.OGR_L_CreateFeature(layer, feat))

        check_err(lgdal.OGR_L_SyncToDisk(layer))
Ejemplo n.º 8
0
def shape_write(iterable, model, columns, get_geom, geom_type, srid, srid_out=None):
    """
    Write tempfile with shape layer.
    """
    from . import field_as_string

    tmp_file, layer, ds, native_srs, output_srs = create_shape_format_layer(columns, geom_type, srid, srid_out)

    feature_def = layer.GetLayerDefn()

    transform = lambda ogr_geom: ogr_geom

    # omg.. will not work for 3D coords
    if native_srs != output_srs:
        ct = osr.CoordinateTransformation(native_srs, output_srs)

        def transform(ogr_geom):
            ogr_geom.Transform(ct)
            return ogr_geom

    for item in iterable:
        feat = ogr.Feature(feature_def)

        for fieldname in columns:
            # They are all String (see create_shape_format_layer)
            value = field_as_string(item, fieldname, ascii=True)
            feat.SetField(fieldname[:10], value[:255])

        geom = get_geom(item)
        if geom:
            ogr_geom = transform(ogr.CreateGeometryFromWkt(geom.wkt))
            check_err(feat.SetGeometry(ogr_geom))

        check_err(layer.CreateFeature(feat))

    ds.Destroy()

    return tmp_file.name
Ejemplo n.º 9
0
    def write_with_native_bindings(self, tmp_name, queryset, geo_field):
        """Scrive un file in un formato geografico da un geoqueryset; questa
        funzione usa le librerie Python di GDAL.

        Scritto da Jared Kibele e Dane Springmeyer.
        """
        dr = ogr.GetDriverByName(str(self.out_format))
        ds = dr.CreateDataSource(tmp_name)
        if ds is None:
            raise Exception('Could not create file!')

        if hasattr(geo_field, 'geom_type'):
            ogr_type = OGRGeomType(geo_field.geom_type).num
        else:
            ogr_type = OGRGeomType(geo_field._geom).num

        native_srs = osr.SpatialReference()
        if hasattr(geo_field, 'srid'):
            native_srs.ImportFromEPSG(geo_field.srid)
        else:
            native_srs.ImportFromEPSG(geo_field._srid)

        if self.proj_transform:
            output_srs = osr.SpatialReference()
            output_srs.ImportFromEPSG(self.proj_transform)
        else:
            output_srs = native_srs

        layer = ds.CreateLayer('lyr', srs=output_srs, geom_type=ogr_type)

        attributes = self.get_attributes()

        for field in attributes:
            field_defn = ogr.FieldDefn(str(field.name), ogr.OFTString)
            field_defn.SetWidth(255)
            if layer.CreateField(field_defn) != 0:
                raise Exception('Faild to create field')

        feature_def = layer.GetLayerDefn()

        for item in queryset:
            feat = ogr.Feature(feature_def)

            for field in attributes:
                value = getattr(item, field.name)
                try:
                    string_value = str(value)
                except UnicodeEncodeError, E:
                    string_value = ''
                feat.SetField(str(field.name), string_value)

            geom = getattr(item, geo_field.name)

            if geom:
                ogr_geom = ogr.CreateGeometryFromWkt(geom.wkt)
                if self.proj_transform:
                    ct = osr.CoordinateTransformation(native_srs, output_srs)
                    ogr_geom.Transform(ct)
                check_err(feat.SetGeometry(ogr_geom))
            else:
                pass

            check_err(layer.CreateFeature(feat))
Ejemplo n.º 10
0
    def write_with_native_bindings(self, tmp_name, queryset, geo_field):
        """ Write a shapefile out to a file from a geoqueryset.

        Written by Jared Kibele and Dane Springmeyer.

        In this case we use the python bindings available with a build
        of gdal when compiled with --with-python, instead of the ctypes-based
        bindings that GeoDjango provides.

        """

        dr = ogr.GetDriverByName('ESRI Shapefile')
        ds = dr.CreateDataSource(tmp_name)
        if ds is None:
            raise Exception('Could not create file!')

        if hasattr(geo_field, 'geom_type'):
            ogr_type = OGRGeomType(geo_field.geom_type).num
        else:
            ogr_type = OGRGeomType(geo_field._geom).num

        native_srs = osr.SpatialReference()
        if hasattr(geo_field, 'srid'):
            native_srs.ImportFromEPSG(geo_field.srid)
        else:
            native_srs.ImportFromEPSG(geo_field._srid)

        if self.proj_transform:
            output_srs = osr.SpatialReference()
            output_srs.ImportFromEPSG(self.proj_transform)
        else:
            output_srs = native_srs

        layer = ds.CreateLayer('lyr', srs=output_srs, geom_type=ogr_type)

        attributes = self.get_attributes()

        for field in attributes:
            # map django field type to OGR type
            field_defn = self.map_ogr_types(field)
            if layer.CreateField(field_defn) != 0:
                raise Exception('Failed to create field')

        feature_def = layer.GetLayerDefn()

        for item in queryset:
            feat = ogr.Feature(feature_def)

            for field in attributes:
                # if the field is a foreign key, return its pk value. this is
                # a problem when a model is given a __unicode__ representation.
                value = getattr(item, field.name)
                if value is not None and isinstance(field, ForeignKey):
                    value = getattr(value, 'pk')
                if isinstance(value, unicode):
                    value = value.encode('utf-8')

                # truncate the field name.
                # TODO: handle nonunique truncated field names.
                feat.SetField(str(field.name[0:10]), value)

            geom = getattr(item, geo_field.name)

            if geom:
                ogr_geom = ogr.CreateGeometryFromWkt(geom.wkt)
                if self.proj_transform:
                    ct = osr.CoordinateTransformation(native_srs, output_srs)
                    ogr_geom.Transform(ct)
                check_err(feat.SetGeometry(ogr_geom))
            else:
                pass

            check_err(layer.CreateFeature(feat))

        ds.Destroy()
Ejemplo n.º 11
0
    def write_with_native_bindings(self,tmp_name,queryset,geo_field):
        """ Write a shapefile out to a file from a geoqueryset.
        
        Written by Jared Kibele and Dane Springmeyer.
        
        In this case we use the python bindings available with a build
        of gdal when compiled with --with-python, instead of the ctypes-based 
        bindings that GeoDjango provides.
        
        """
        
        dr = ogr.GetDriverByName('ESRI Shapefile')
        ds = dr.CreateDataSource(tmp_name)
        if ds is None:
            raise Exception('Could not create file!')
        
        if hasattr(geo_field,'geom_type'):
            ogr_type = OGRGeomType(geo_field.geom_type).num
        else:
            ogr_type = OGRGeomType(geo_field._geom).num

        native_srs = osr.SpatialReference()
        if hasattr(geo_field,'srid'):
            native_srs.ImportFromEPSG(geo_field.srid)
        else:
            native_srs.ImportFromEPSG(geo_field._srid)
        
        if self.proj_transform:
            output_srs = osr.SpatialReference()
            output_srs.ImportFromEPSG(self.proj_transform)
        else:
            output_srs = native_srs

        layer = ds.CreateLayer('lyr',srs=output_srs,geom_type=ogr_type)
        
        attributes = self.get_attributes()
        
        for field in attributes:
            field_defn = ogr.FieldDefn(str(field.name),ogr.OFTString)
            field_defn.SetWidth( 255 )
            if layer.CreateField(field_defn) != 0:
                raise Exception('Faild to create field')
        
        feature_def = layer.GetLayerDefn()
        
        for item in queryset:
            feat = ogr.Feature( feature_def )
            
            for field in attributes:
                value = getattr(item,field.name)
                try:
                    string_value = str(value)
                except UnicodeEncodeError, E:
                    string_value = ''
                feat.SetField(str(field.name),string_value)
              
            geom = getattr(item,geo_field.name)
            
            if geom:
                ogr_geom = ogr.CreateGeometryFromWkt(geom.wkt)
                if self.proj_transform:
                    ct = osr.CoordinateTransformation(native_srs, output_srs)
                    ogr_geom.Transform(ct)
                check_err(feat.SetGeometry(ogr_geom))
            else:
                pass

            check_err(layer.CreateFeature(feat))
Ejemplo n.º 12
0
     for idx,field in enumerate(attributes):
         value = getattr(item,field.name)
         try:
             string_value = str(value)
         except UnicodeEncodeError, E:
           
             string_value = ''
         lgdal.OGR_F_SetFieldString(feat, idx, string_value)
       
     # Transforming & setting the geometry
     geom = getattr(item,geo_field.name)
     
     # if requested we transform the input geometry
     # to match the shapefiles projection 'to-be'            
     if geom:
         ogr_geom = OGRGeometry(geom.wkt,output_srs)
         if self.proj_transform:
             ct = CoordTransform(native_srs, output_srs)
             ogr_geom.transform(ct)
         # create the geometry
         check_err(lgdal.OGR_F_SetGeometry(feat, ogr_geom._ptr))
     else:
         pass
     
     # creat the feature in the layer.
     check_err(lgdal.OGR_L_SetFeature(layer, feat))
 
 # Cleaning up
 check_err(lgdal.OGR_L_SyncToDisk(layer))
 lgdal.OGR_DS_Destroy(ds)
 lgdal.OGRCleanupAll()
Ejemplo n.º 13
0
    def write_with_ctypes_bindings(self,tmp_name,queryset,geo_field):
        """ Write a shapefile out to a file from a geoqueryset.
        
        Uses GeoDjangos ctypes wrapper to ogr in libgdal.
        
        """

        # Get the shapefile driver
        dr = Driver('ESRI Shapefile')
        
        # Creating the datasource
        ds = lgdal.OGR_Dr_CreateDataSource(dr._ptr, tmp_name, None)
        if ds is None:
            raise Exception('Could not create file!')
                
        # Get the right geometry type number for ogr
        if hasattr(geo_field,'geom_type'):
            ogr_type = OGRGeomType(geo_field.geom_type).num
        else:
            ogr_type = OGRGeomType(geo_field._geom).num

        # Set up the native spatial reference of the geometry field using the srid
        if hasattr(geo_field,'srid'):
            native_srs = SpatialReference(geo_field.srid)
        else:
            native_srs = SpatialReference(geo_field._srid)
        
        if self.proj_transform:
            output_srs = SpatialReference(self.proj_transform)
        else:
            output_srs = native_srs
        
        # create the layer
        # this is crashing python26 on osx and ubuntu
        layer = lgdal.OGR_DS_CreateLayer(ds, 'lyr', output_srs._ptr, ogr_type, None)
        
        # Create the fields
        attributes = self.get_attributes()

        for field in attributes:
            fld = lgdal.OGR_Fld_Create(str(field.name), 4)
            added = lgdal.OGR_L_CreateField(layer, fld, 0)
            check_err(added) 
        
        # Getting the Layer feature definition.
        feature_def = lgdal.OGR_L_GetLayerDefn(layer) 
        
        # Loop through queryset creating features
        for item in self.queryset:
            feat = lgdal.OGR_F_Create(feature_def)

                                   
            for idx,field in enumerate(attributes):
                value = getattr(item,field.name)
                try:
                    string_value = str(value)
                except UnicodeEncodeError, E:
                  
                    string_value = ''
                lgdal.OGR_F_SetFieldString(feat, idx, string_value)
              
            # Transforming & setting the geometry
            geom = getattr(item,geo_field.name)
            
            # if requested we transform the input geometry
            # to match the shapefiles projection 'to-be'            
            if geom:
                ogr_geom = OGRGeometry(geom.wkt,output_srs)
                if self.proj_transform:
                    ct = CoordTransform(native_srs, output_srs)
                    ogr_geom.transform(ct)
                # create the geometry
                check_err(lgdal.OGR_F_SetGeometry(feat, ogr_geom._ptr))
            else:
                pass
            
            # creat the feature in the layer.
            check_err(lgdal.OGR_L_SetFeature(layer, feat))
Ejemplo n.º 14
0
def shape_write(iterable, model, columns, get_geom, geom_type, srid, srid_out=None):
    """
    Write tempfile with shape layer.
    """

    headers = []
    columns_headers = {}
    for field in columns:
        c = getattr(model, '%s_verbose_name' % field, None)
        if c is None:
            try:
                f = model._meta.get_field(field)
                if f.one_to_many:
                    c = f.field.model._meta.verbose_name_plural
                else:
                    c = f.verbose_name
            except FieldDoesNotExist:
                c = _(field.title())

        reponse = "{}".format(c)
        reponse = unicodedata.normalize('NFD', reponse)
        reponse = smart_str(reponse.encode('ascii', 'ignore')).replace(' ', '_').lower()

        headers.append(reponse)
        columns_headers[field] = reponse

    tmp_file, layer, ds, native_srs, output_srs, column_map = create_shape_format_layer(headers, geom_type,
                                                                                        srid, srid_out)

    feature_def = layer.GetLayerDefn()

    if native_srs != output_srs:
        ct = osr.CoordinateTransformation(native_srs, output_srs)

        def transform(ogr_geom):
            ogr_geom.Transform(ct)
            return ogr_geom
    else:
        def transform(ogr_geom):
            return ogr_geom

    for item in iterable:
        feat = ogr.Feature(feature_def)

        for fieldname in columns:
            try:
                modelfield = model._meta.get_field(fieldname)
            except FieldDoesNotExist:
                modelfield = None
            if isinstance(modelfield, ForeignKey):
                value = smart_plain_text(getattr(item, fieldname))
            elif isinstance(modelfield, ManyToManyField):
                value = ','.join([smart_plain_text(o)
                                  for o in getattr(item, fieldname).all()] or '')
            else:
                value = field_as_string(item, fieldname)

            feat.SetField(column_map.get(columns_headers.get(fieldname)),
                          value[:254])

        geom = get_geom(item)
        if geom:
            ogr_geom = transform(ogr.CreateGeometryFromWkt(geom.wkt))
            check_err(feat.SetGeometry(ogr_geom))

        check_err(layer.CreateFeature(feat))

    ds.Destroy()

    return tmp_file.name
Ejemplo n.º 15
0
    def __call__(self):
        """
        """
        fields = self.queryset.model._meta.fields
        geo_fields = [f for f in fields if isinstance(f, GeometryField)]
        geo_fields_names = ", ".join([f.name for f in geo_fields])
        attributes = [f for f in fields if not isinstance(f, GeometryField)]

        if len(geo_fields) > 1:
            if not self.geo_field:
                raise ValueError(
                    "More than one geodjango geometry field found, please specify which to use by name. Available fields are: '%s'"
                    % geo_fields_names
                )
            else:
                geo_field_by_name = [fld for fld in geo_fields if fld.name == self.geo_field]
                if not geo_field_by_name:
                    raise ValueError(
                        "Geodjango geometry field not found with the name '%s', fields available are: '%s'"
                        % (self.geo_field, geo_fields_names)
                    )
                else:
                    geo_field = geo_field_by_name[0]
        elif geo_fields:
            geo_field = geo_fields[0]
        else:
            raise ValueError("No geodjango geometry fields found in this model queryset")

        ogr.OGRRegisterAll()
        # Get the shapefile driver
        dr = ogr.OGRGetDriverByName("ESRI Shapefile")

        # create a temporary file to write the shapefile to
        # since we are ultimately going to zip it up
        tmp = tempfile.NamedTemporaryFile(suffix=".shp", mode="w+b")
        # we must close the file for GDAL to be able to open and write to it
        tmp.close()

        # Creating the datasource
        ds = ogr.OGR_Dr_CreateDataSource(dr, tmp.name, None)

        # Get the right geometry type number for ogr
        ogr_type = OGRGeomType(geo_field._geom).num

        # Set up the spatial reference with epsg code
        srs = SpatialReference(geo_field._srid)

        # If true we're going to reproject later on
        if self.proj_transform:
            srs = SpatialReference(self.proj_transform)

        # Creating the layer
        layer = ogr.OGR_DS_CreateLayer(ds, os.path.basename(tmp.name), srs._ptr, ogr_type, None)

        # Create the fields
        # Todo: control field order as param
        for field in attributes:
            fld = ogr.OGR_Fld_Create(str(field.name), 4)
            added = ogr.OGR_L_CreateField(layer, fld, 0)
            check_err(added)

        # Getting the Layer feature definition.
        feature_def = ogr.OGR_L_GetLayerDefn(layer)

        # Loop through queryset creating features
        for item in self.queryset:
            feat = ogr.OGR_F_Create(feature_def)

            # For now, set all fields as strings
            # TODO: catch model types and convert to ogr fields
            # http://www.gdal.org/ogr/classOGRFeature.html

            # OGR_F_SetFieldDouble
            # OFTReal => FloatField DecimalField

            # OGR_F_SetFieldInteger
            # OFTInteger => IntegerField

            # OGR_F_SetFieldStrin
            # OFTString => CharField

            # OGR_F_SetFieldDateTime()
            # OFTDateTime => DateTimeField
            # OFTDate => TimeField
            # OFTDate => DateField

            idx = 0
            for field in attributes:
                value = getattr(item, field.name)
                try:
                    string_value = str(value)
                except UnicodeEncodeError, E:
                    # pass for now....
                    # http://trac.osgeo.org/gdal/ticket/882
                    string_value = ""
                ogr.OGR_F_SetFieldString(feat, idx, string_value)
                idx += 1

            # Transforming & setting the geometry
            geom = getattr(item, geo_field.name)
            # import pdb;pdb.set_trace()
            # if requested we transform the input geometry
            # to match the shapefiles projection 'to-be'

            if geom:
                if self.proj_transform:
                    geom.transform(self.proj_transform)
                ogr_geom = OGRGeometry(geom.wkt, srs)
                # create the geometry
                check_err(ogr.OGR_F_SetGeometry(feat, ogr_geom._ptr))
            else:
                # Case where geometry object is not found because of null value for field
                # effectively looses whole record in shapefile if geometry does not exist
                pass

            # creat the feature in the layer.
            check_err(ogr.OGR_L_SetFeature(layer, feat))
Ejemplo n.º 16
0
def shape_write(iterable,
                model,
                columns,
                get_geom,
                geom_type,
                srid,
                srid_out=None):
    """
    Write tempfile with shape layer.
    """

    headers = []
    columns_headers = {}
    for field in columns:
        c = getattr(model, '%s_verbose_name' % field, None)
        if c is None:
            try:
                f = model._meta.get_field(field)
                if f.one_to_many:
                    c = f.field.model._meta.verbose_name_plural
                else:
                    c = f.verbose_name
            except FieldDoesNotExist:
                c = _(field.title())

        reponse = "{}".format(c)
        reponse = unicodedata.normalize('NFD', reponse)
        reponse = smart_str(reponse.encode('ascii',
                                           'ignore')).replace(' ',
                                                              '_').lower()

        headers.append(reponse)
        columns_headers[field] = reponse

    tmp_file, layer, ds, native_srs, output_srs, column_map = create_shape_format_layer(
        headers, geom_type, srid, srid_out)

    feature_def = layer.GetLayerDefn()

    if native_srs != output_srs:
        ct = osr.CoordinateTransformation(native_srs, output_srs)

        def transform(ogr_geom):
            ogr_geom.Transform(ct)
            return ogr_geom
    else:

        def transform(ogr_geom):
            return ogr_geom

    for item in iterable:
        feat = ogr.Feature(feature_def)

        for fieldname in columns:
            try:
                modelfield = model._meta.get_field(fieldname)
            except FieldDoesNotExist:
                modelfield = None
            if isinstance(modelfield, ForeignKey):
                value = smart_plain_text(getattr(item, fieldname))
            elif isinstance(modelfield, ManyToManyField):
                value = ','.join([
                    smart_plain_text(o)
                    for o in getattr(item, fieldname).all()
                ] or '')
            else:
                value = field_as_string(item, fieldname)

            feat.SetField(column_map.get(columns_headers.get(fieldname)),
                          value[:254])

        geom = get_geom(item)
        if geom:
            ogr_geom = transform(ogr.CreateGeometryFromWkt(geom.wkt))
            check_err(feat.SetGeometry(ogr_geom))

        check_err(layer.CreateFeature(feat))

    ds.Destroy()

    return tmp_file.name
Ejemplo n.º 17
0
                feat.SetField(idx, value)
                idx += 1

            # Transforming & setting the geometry
            geom = getattr(item,geo_field.name)

            # if requested we transform the input geometry
            # to match the shapefiles projection 'to-be'            
            if geom:
                ###ogr_geom = OGRGeometry(geom.wkt,output_srs)
                ogr_geom = ogr.CreateGeometryFromWkt(geom.wkt)
                ###if self.proj_transform:
                ###    ogr_geom.transform(ct)
                # create the geometry
                ###check_err(ogr.OGR_F_SetGeometry(feat, ogr_geom._ptr))
                check_err(feat.SetGeometry(ogr_geom))
            else:
                # Case where geometry object is not found because of null value for field
                # effectively looses whole record in shapefile if geometry does not exist
                pass

            # creat the feature in the layer.
            ###check_err(ogr.OGR_L_SetFeature(layer, feat))
            check_err(layer.CreateFeature(feat))

        # Cleaning up
        ###check_err(ogr.OGR_L_SyncToDisk(layer))
        ###ogr.OGR_DS_Destroy(ds)
        ###ogr.OGRCleanupAll()
        ds.Destroy()
Ejemplo n.º 18
0
                    # pass for now....
                    # http://trac.osgeo.org/gdal/ticket/882
                    string_value = ''
                lgdal.OGR_F_SetFieldString(feat, idx, string_value)

            # Transforming & setting the geometry
            geom = getattr(item, geo_field.name)

            # if requested we transform the input geometry
            # to match the shapefiles projection 'to-be'
            if geom:
                ogr_geom = OGRGeometry(geom.wkt, output_srs)
                if self.proj_transform:
                    ct = CoordTransform(native_srs, output_srs)
                    ogr_geom.transform(ct)
                # create the geometry
                check_err(lgdal.OGR_F_SetGeometry(feat, ogr_geom._ptr))
            else:
                # Case where geometry object is not found because of null
                # value for field effectively looses whole record in shapefile
                # if geometry does not exist
                pass

            # creat the feature in the layer.
            check_err(lgdal.OGR_L_SetFeature(layer, feat))

        # Cleaning up
        check_err(lgdal.OGR_L_SyncToDisk(layer))
        lgdal.OGR_DS_Destroy(ds)
        lgdal.OGRCleanupAll()
Ejemplo n.º 19
0
    def write_with_ctypes_bindings(self,tmp_name,queryset,geo_field):
        """ Write a shapefile out to a file from a geoqueryset.
        
        Uses GeoDjangos ctypes wrapper to ogr in libgdal.
        
        """

        # Get the shapefile driver
        dr = Driver('ESRI Shapefile')
        # Creating the datasource
        ds = lgdal.OGR_Dr_CreateDataSource(dr._ptr, tmp_name, None)
        if ds is None:
            raise Exception('Could not create file!')
                
        # Get the right geometry type number for ogr
        if hasattr(geo_field,'geom_type'):
            ogr_type = OGRGeomType(geo_field.geom_type).num
        else:
            ogr_type = OGRGeomType(geo_field._geom).num
        # Set up the native spatial reference of the geometry field using the srid
        if hasattr(geo_field,'srid'):
            native_srs = SpatialReference(geo_field.srid)
        else:
            native_srs = SpatialReference(geo_field._srid)
        if self.proj_transform:
            output_srs = SpatialReference(self.proj_transform)
        else:
            output_srs = native_srs
        
        # create the layer
        # this is crashing python26 on osx and ubuntu
        layer = lgdal.OGR_DS_CreateLayer(ds, 'lyr', output_srs._ptr, ogr_type, None)
        
        # Create the fields
        attributes = self.get_attributes()

        for field in attributes:
            fld = lgdal.OGR_Fld_Create(str(field.name), 4)
            added = lgdal.OGR_L_CreateField(layer, fld, 0)
            check_err(added)
        
        # Getting the Layer feature definition.
        feature_def = lgdal.OGR_L_GetLayerDefn(layer)
        # Loop through queryset creating features
        for item in self.queryset:
            feat = lgdal.OGR_F_Create(feature_def)

            # For now, set all fields as strings
            # TODO: catch model types and convert to ogr fields
            # http://www.gdal.org/ogr/classOGRFeature.html
            
            # OGR_F_SetFieldDouble
            #OFTReal => FloatField DecimalField
            
            # OGR_F_SetFieldInteger
            #OFTInteger => IntegerField
            
            #OGR_F_SetFieldStrin
            #OFTString => CharField
            
            
            # OGR_F_SetFieldDateTime()
            #OFTDateTime => DateTimeField
            #OFTDate => TimeField
            #OFTDate => DateField
            for idx,field in enumerate(attributes):
                value = getattr(item,field.name)
                try:
                    string_value = str(value)
                except UnicodeEncodeError, E:
                    # pass for now....
                    # http://trac.osgeo.org/gdal/ticket/882
                    string_value = ''
                lgdal.OGR_F_SetFieldString(feat, idx, string_value)
              
            # Transforming & setting the geometry
            geom = getattr(item,geo_field.name)
            
            # if requested we transform the input geometry
            # to match the shapefiles projection 'to-be'            
            if geom:
                ogr_geom = OGRGeometry(geom.wkt,output_srs)
                if self.proj_transform:
                    ct = CoordTransform(native_srs, output_srs)
                    ogr_geom.transform(ct)
                # create the geometry
                check_err(lgdal.OGR_F_SetGeometry(feat, ogr_geom._ptr))
            else:
                # Case where geometry object is not found because of null value for field
                # effectively looses whole record in shapefile if geometry does not exist
                pass
            
            
            # creat the feature in the layer.
            check_err(lgdal.OGR_L_SetFeature(layer, feat))
Ejemplo n.º 20
0
    def __call__(self, *args, **kwargs):
        """
        """
        fields = self.queryset.model._meta.fields
        geo_fields = [f for f in fields if isinstance(f, GeometryField)]
        geo_fields_names = ', '.join([f.name for f in geo_fields])
        attributes = [f for f in fields if not isinstance(f, GeometryField)]
        
        if len(geo_fields) > 1:
            if not self.geo_field:
                raise ValueError("More than one geodjango geometry field found, please specify which to use by name using the 'geo_field' keyword. Available fields are: '%s'" % geo_fields_names)
            else:
                geo_field_by_name = [fld for fld in geo_fields if fld.name == self.geo_field]
                if not geo_field_by_name:
                    raise ValueError("Geodjango geometry field not found with the name '%s', fields available are: '%s'" % (self.geo_field,geo_fields_names))
                else:
                    geo_field = geo_field_by_name[0]
        elif geo_fields:
            geo_field = geo_fields[0]
        else:
            raise ValueError('No geodjango geometry fields found in this model queryset')
        
        # Get the shapefile driver
        ###dr = Driver('ESRI Shapefile')
        dr = ogr.GetDriverByName('ESRI Shapefile')
        
        # create a temporary file to write the shapefile to
        # since we are ultimately going to zip it up
        tmp = tempfile.NamedTemporaryFile(suffix='.shp', mode = 'w+b')
        # we must close the file for GDAL to be able to open and write to it
        tmp.close()
        
        # Creating the datasource
        ###ds = ogr.OGR_Dr_CreateDataSource(dr._ptr, tmp.name, None)
        ds = dr.CreateDataSource(tmp.name)
        if ds is None:
            raise Exception('Could not create file!')
        
        # Get the right geometry type number for ogr
        if hasattr(geo_field,'geom_type'):
            ###ogr_type = OGRGeomType(geo_field.geom_type).num
            ogr_type = OGRGeomType(geo_field.geom_type).num
        else:
            ###ogr_type = OGRGeomType(geo_field._geom).num
            ogr_type = OGRGeomType(geo_field._geom).num

        # Set up the native spatial reference of the geometry field using the srid    
        native_srs = osr.SpatialReference()
        if hasattr(geo_field,'srid'):
            ###native_srs = SpatialReference(geo_field.srid)
            native_srs.ImportFromEPSG(geo_field.srid)
        else:
            ###native_srs = SpatialReference(geo_field._srid)
            native_srs.ImportFromEPSG(geo_field._srid)
        
        ###if self.proj_transform:
        ###    output_srs = SpatialReference(self.proj_transform)
        ###    ct = CoordTransform(native_srs, output_srs)
        ###else:
        ###    output_srs = native_srs
            
        output_srs = native_srs
        
        # create the layer
        # print 'about to try to create data layer'
        # print 'ds: %s, path: %s' % (ds, tmp.name)
        ###layer = ogr.OGR_DS_CreateLayer(ds, tmp.name, output_srs._ptr, ogr_type, None)
        layer = ds.CreateLayer('lyr',srs=output_srs,geom_type=ogr_type)
        
        # Create the fields
        # Todo: control field order as param
        for field in attributes:
            ###fld = ogr.OGR_Fld_Create(str(field.name), 4)
            ###added = ogr.OGR_L_CreateField(layer, fld, 0)
            ###check_err(added) 
            
            if field.__class__.__name__ == 'FloatField':
                field_defn = ogr.FieldDefn(str(field.name),ogr.OFTReal)
            elif field.__class__.__name__ == 'IntegerField':
                field_defn = ogr.FieldDefn(str(field.name),ogr.OFTInteger)
            else:
                field_defn = ogr.FieldDefn(str(field.name),ogr.OFTString)
            field_defn.SetWidth( 255 )
            if layer.CreateField(field_defn) != 0:
                raise Exception('Faild to create field')
        
        # Getting the Layer feature definition.
        ###feature_def = ogr.OGR_L_GetLayerDefn(layer) 
        feature_def = layer.GetLayerDefn()
        
        # Loop through queryset creating features
        for item in self.queryset:
            ###feat = ogr.OGR_F_Create(feature_def)
            feat = ogr.Feature( feature_def )
            
            # For now, set all fields as strings
            # TODO: catch model types and convert to ogr fields
            # http://www.gdal.org/ogr/classOGRFeature.html
            
            # OGR_F_SetFieldDouble
            #OFTReal => FloatField DecimalField
            
            # OGR_F_SetFieldInteger
            #OFTInteger => IntegerField
            
            #OGR_F_SetFieldStrin
            #OFTString => CharField
            
            
            # OGR_F_SetFieldDateTime()
            #OFTDateTime => DateTimeField
            #OFTDate => TimeField
            #OFTDate => DateField
            
            idx = 0
            for field in attributes:
                value = getattr(item,field.name)

                if field.__class__.__name__ == 'FloatField':
                    value = float(value)
                elif field.__class__.__name__ == 'IntegerField':
                    value = int(value)
                else:
                    try:
                        value = str(value)
                    except UnicodeEncodeError, E:
                        # http://trac.osgeo.org/gdal/ticket/882
                        value = ''

                ###ogr.OGR_F_SetFieldString(feat, idx, string_value)
                #changing the following SetField command from accessing field by name to index
                #this change solves an issue that arose sometime after gdal 1.6.3
                #in which the field names became truncated to 10 chars in CreateField
                #feat.SetField(str(field.name),string_value)
                feat.SetField(idx, value)
                idx += 1
              
            # Transforming & setting the geometry
            geom = getattr(item,geo_field.name)
            
            # if requested we transform the input geometry
            # to match the shapefiles projection 'to-be'            
            if geom:
                ###ogr_geom = OGRGeometry(geom.wkt,output_srs)
                ogr_geom = ogr.CreateGeometryFromWkt(geom.wkt)
                ###if self.proj_transform:
                ###    ogr_geom.transform(ct)
                # create the geometry
                ###check_err(ogr.OGR_F_SetGeometry(feat, ogr_geom._ptr))
                check_err(feat.SetGeometry(ogr_geom))
            else:
                # Case where geometry object is not found because of null value for field
                # effectively looses whole record in shapefile if geometry does not exist
                pass
            
            
            # creat the feature in the layer.
            ###check_err(ogr.OGR_L_SetFeature(layer, feat))
            check_err(layer.CreateFeature(feat))
Ejemplo n.º 21
0
    def __call__(self):
        """
        """
        fields = self.queryset.model._meta.fields
        geo_fields = [f for f in fields if isinstance(f, GeometryField)]
        geo_fields_names = ', '.join([f.name for f in geo_fields])
        attributes = [f for f in fields if not isinstance(f, GeometryField)]
        
        if len(geo_fields) > 1:
            if not self.geo_field:
              raise ValueError("More than one geodjango geometry field found, please specify which to use by name. Available fields are: '%s'" % geo_fields_names)
            else:
              geo_field_by_name = [fld for fld in geo_fields if fld.name == self.geo_field]
              if not geo_field_by_name:
                raise ValueError("Geodjango geometry field not found with the name '%s', fields available are: '%s'" % (self.geo_field,geo_fields_names))
              else:
                geo_field = geo_field_by_name[0]
        elif geo_fields:
            geo_field = geo_fields[0]
        else:
            raise ValueError('No geodjango geometry fields found in this model queryset')
        
        ogr.OGRRegisterAll()
        # Get the shapefile driver
        dr = ogr.OGRGetDriverByName('ESRI Shapefile')
        
        # create a temporary file to write the shapefile to
        # since we are ultimately going to zip it up
        tmp = tempfile.NamedTemporaryFile(suffix='.shp', mode = 'w+b')
        # we must close the file for GDAL to be able to open and write to it
        tmp.close()
        
        # Creating the datasource
        ds = ogr.OGR_Dr_CreateDataSource(dr,tmp.name, None)
        
        # Get the right geometry type number for ogr
        ogr_type = OGRGeomType(geo_field._geom).num

        # Set up the spatial reference with epsg code
        srs = SpatialReference(geo_field._srid)
        
        # If true we're going to reproject later on 
        if self.proj_transform:
          srs = SpatialReference(self.proj_transform)
        
        # Creating the layer
        layer = ogr.OGR_DS_CreateLayer(ds,os.path.basename(tmp.name), srs._ptr, ogr_type, None)
        
        # Create the fields
        # Todo: control field order as param
        for field in attributes:
          fld = ogr.OGR_Fld_Create(str(field.name), 4)
          added = ogr.OGR_L_CreateField(layer, fld, 0)
          check_err(added) 
        
        # Getting the Layer feature definition.
        feature_def = ogr.OGR_L_GetLayerDefn(layer) 
        
        # Loop through queryset creating features
        for item in self.queryset:
            feat = ogr.OGR_F_Create(feature_def)
            
            # For now, set all fields as strings
            # TODO: catch model types and convert to ogr fields
            # http://www.gdal.org/ogr/classOGRFeature.html
            
            # OGR_F_SetFieldDouble
            #OFTReal => FloatField DecimalField
            
            # OGR_F_SetFieldInteger
            #OFTInteger => IntegerField
            
            #OGR_F_SetFieldStrin
            #OFTString => CharField
            
            
            # OGR_F_SetFieldDateTime()
            #OFTDateTime => DateTimeField
            #OFTDate => TimeField
            #OFTDate => DateField
            
            idx = 0
            for field in attributes:
              value = getattr(item,field.name)
              try:
                string_value = str(value)
              except UnicodeEncodeError, E:
                # pass for now....
                # http://trac.osgeo.org/gdal/ticket/882
                string_value = ''
              ogr.OGR_F_SetFieldString(feat, idx, string_value)
              idx += 1
              
            # Transforming & setting the geometry
            geom = getattr(item,geo_field.name)
            #import pdb;pdb.set_trace()
            # if requested we transform the input geometry
            # to match the shapefiles projection 'to-be'
            
            if geom:
              if self.proj_transform:
                geom.transform(self.proj_transform)
              ogr_geom = OGRGeometry(geom.wkt,srs)
              # create the geometry
              check_err(ogr.OGR_F_SetGeometry(feat, ogr_geom._ptr))
            else:
              # Case where geometry object is not found because of null value for field
              # effectively looses whole record in shapefile if geometry does not exist
              pass
            
            
            # creat the feature in the layer.
            check_err(ogr.OGR_L_SetFeature(layer, feat))
Ejemplo n.º 22
0
class ShpResponder(object):
    def __init__(self, queryset, readme=None, geo_field=None, proj_transform=None, mimetype='application/zip',file_name='shp_download'):
        """
        """
        self.queryset = queryset
        self.readme = readme
        self.geo_field = geo_field
        self.proj_transform = proj_transform
        self.mimetype = mimetype
        self.file_name = file_name
    
    def __call__(self):
        """
        """
        fields = self.queryset.model._meta.fields
        geo_fields = [f for f in fields if isinstance(f, GeometryField)]
        geo_fields_names = ', '.join([f.name for f in geo_fields])
        attributes = [f for f in fields if not isinstance(f, GeometryField)]
        
        if len(geo_fields) > 1:
            if not self.geo_field:
              raise ValueError("More than one geodjango geometry field found, please specify which to use by name. Available fields are: '%s'" % geo_fields_names)
            else:
              geo_field_by_name = [fld for fld in geo_fields if fld.name == self.geo_field]
              if not geo_field_by_name:
                raise ValueError("Geodjango geometry field not found with the name '%s', fields available are: '%s'" % (self.geo_field,geo_fields_names))
              else:
                geo_field = geo_field_by_name[0]
        elif geo_fields:
            geo_field = geo_fields[0]
        else:
            raise ValueError('No geodjango geometry fields found in this model queryset')
        
        ogr.OGRRegisterAll()
        # Get the shapefile driver
        dr = ogr.OGRGetDriverByName('ESRI Shapefile')
        
        # create a temporary file to write the shapefile to
        # since we are ultimately going to zip it up
        tmp = tempfile.NamedTemporaryFile(suffix='.shp', mode = 'w+b')
        # we must close the file for GDAL to be able to open and write to it
        tmp.close()
        
        # Creating the datasource
        ds = ogr.OGR_Dr_CreateDataSource(dr,tmp.name, None)
        
        # Get the right geometry type number for ogr
        ogr_type = OGRGeomType(geo_field._geom).num

        # Set up the spatial reference with epsg code
        srs = SpatialReference(geo_field._srid)
        
        # If true we're going to reproject later on 
        if self.proj_transform:
          srs = SpatialReference(self.proj_transform)
        
        # Creating the layer
        layer = ogr.OGR_DS_CreateLayer(ds,os.path.basename(tmp.name), srs._ptr, ogr_type, None)
        
        # Create the fields
        # Todo: control field order as param
        for field in attributes:
          fld = ogr.OGR_Fld_Create(str(field.name), 4)
          added = ogr.OGR_L_CreateField(layer, fld, 0)
          check_err(added) 
        
        # Getting the Layer feature definition.
        feature_def = ogr.OGR_L_GetLayerDefn(layer) 
        
        # Loop through queryset creating features
        for item in self.queryset:
            feat = ogr.OGR_F_Create(feature_def)
            
            # For now, set all fields as strings
            # TODO: catch model types and convert to ogr fields
            # http://www.gdal.org/ogr/classOGRFeature.html
            
            # OGR_F_SetFieldDouble
            #OFTReal => FloatField DecimalField
            
            # OGR_F_SetFieldInteger
            #OFTInteger => IntegerField
            
            #OGR_F_SetFieldStrin
            #OFTString => CharField
            
            
            # OGR_F_SetFieldDateTime()
            #OFTDateTime => DateTimeField
            #OFTDate => TimeField
            #OFTDate => DateField
            
            idx = 0
            for field in attributes:
              value = getattr(item,field.name)
              try:
                string_value = str(value)
              except UnicodeEncodeError, E:
                # pass for now....
                # http://trac.osgeo.org/gdal/ticket/882
                string_value = ''
              ogr.OGR_F_SetFieldString(feat, idx, string_value)
              idx += 1
              
            # Transforming & setting the geometry
            geom = getattr(item,geo_field.name)
            #import pdb;pdb.set_trace()
            # if requested we transform the input geometry
            # to match the shapefiles projection 'to-be'
            
            if geom:
              if self.proj_transform:
                geom.transform(self.proj_transform)
              ogr_geom = OGRGeometry(geom.wkt,srs)
              # create the geometry
              check_err(ogr.OGR_F_SetGeometry(feat, ogr_geom._ptr))
            else:
              # Case where geometry object is not found because of null value for field
              # effectively looses whole record in shapefile if geometry does not exist
              pass
            
            
            # creat the feature in the layer.
            check_err(ogr.OGR_L_SetFeature(layer, feat))
        
        # Cleaning up
        check_err(ogr.OGR_L_SyncToDisk(layer))
        ogr.OGR_DS_Destroy(ds)
        ogr.OGRCleanupAll()
        
        #Read resulting shapefile into a zipfile buffer
        buffer = cStringIO.StringIO()
        zip = zipfile.ZipFile(buffer, 'w', zipfile.ZIP_DEFLATED)
        files = ['shp','shx','prj','dbf']
        for item in files:
          filename= '%s.%s' % (tmp.name.strip('.shp'), item)
          zip.write(filename, arcname='%s.%s' % (self.file_name.rstrip('.shp'), item))
        if self.readme:
          zip.writestr('README.txt',self.readme)
        zip.close()
        buffer.flush()
        zip_stream = buffer.getvalue()
        buffer.close()
        
        # Stick it all in a django HttpResponse
        response = HttpResponse()
        response['Content-Disposition'] = 'filename=%s.zip' % self.file_name.rstrip('.shp')
        response['Content-length'] = str(len(zip_stream))
        response['Content-Type'] = self.mimetype
        response.write(zip_stream)
        return response
Ejemplo n.º 23
0
    def __call__(self, *args, **kwargs):
        """
        """
        fields = self.queryset.model._meta.fields
        geo_fields = [f for f in fields if isinstance(f, GeometryField)]
        geo_fields_names = ', '.join([f.name for f in geo_fields])
        attributes = [f for f in fields if not isinstance(f, GeometryField)]

        if len(geo_fields) > 1:
            if not self.geo_field:
                raise ValueError("More than one geodjango geometry field found, please specify which to use by name using the 'geo_field' keyword. Available fields are: '%s'" % geo_fields_names)
            else:
                geo_field_by_name = [fld for fld in geo_fields if fld.name == self.geo_field]
                if not geo_field_by_name:
                    raise ValueError("Geodjango geometry field not found with the name '%s', fields available are: '%s'" % (self.geo_field,geo_fields_names))
                else:
                    geo_field = geo_field_by_name[0]
        elif geo_fields:
            geo_field = geo_fields[0]
        else:
            raise ValueError('No geodjango geometry fields found in this model queryset')

        # Get the shapefile driver
        ###dr = Driver('ESRI Shapefile')
        dr = ogr.GetDriverByName('ESRI Shapefile')

        # create a temporary file to write the shapefile to
        # since we are ultimately going to zip it up
        tmp = tempfile.NamedTemporaryFile(suffix='.shp', mode='w+b')
        # we must close the file for GDAL to be able to open and write to it
        tmp.close()

        # Creating the datasource
        ###ds = ogr.OGR_Dr_CreateDataSource(dr._ptr, tmp.name, None)
        ds = dr.CreateDataSource(tmp.name)
        if ds is None:
            raise Exception('Could not create file!')

        # Get the right geometry type number for ogr
        if hasattr(geo_field,'geom_type'):
            ###ogr_type = OGRGeomType(geo_field.geom_type).num
            ogr_type = OGRGeomType(geo_field.geom_type).num
        else:
            ###ogr_type = OGRGeomType(geo_field._geom).num
            ogr_type = OGRGeomType(geo_field._geom).num

        # Set up the native spatial reference of the geometry field using the srid    
        native_srs = osr.SpatialReference()
        if hasattr(geo_field,'srid'):
            ###native_srs = SpatialReference(geo_field.srid)
            native_srs.ImportFromEPSG(geo_field.srid)
        else:
            ###native_srs = SpatialReference(geo_field._srid)
            native_srs.ImportFromEPSG(geo_field._srid)

        ###if self.proj_transform:
        ###    output_srs = SpatialReference(self.proj_transform)
        ###    ct = CoordTransform(native_srs, output_srs)
        ###else:
        ###    output_srs = native_srs

        output_srs = native_srs

        # create the layer
        # print 'about to try to create data layer'
        # print 'ds: %s, path: %s' % (ds, tmp.name)
        ###layer = ogr.OGR_DS_CreateLayer(ds, tmp.name, output_srs._ptr, ogr_type, None)
        layer = ds.CreateLayer('lyr',srs=output_srs,geom_type=ogr_type)

        # Create the fields
        # Todo: control field order as param
        for field in attributes:
            ###fld = ogr.OGR_Fld_Create(str(field.name), 4)
            ###added = ogr.OGR_L_CreateField(layer, fld, 0)
            ###check_err(added) 

            if field.__class__.__name__ == 'FloatField':
                field_defn = ogr.FieldDefn(str(field.name),ogr.OFTReal)
            elif field.__class__.__name__ == 'IntegerField':
                field_defn = ogr.FieldDefn(str(field.name),ogr.OFTInteger)
            else:
                field_defn = ogr.FieldDefn(str(field.name),ogr.OFTString)
            field_defn.SetWidth(255)
            if layer.CreateField(field_defn) != 0:
                raise Exception('Faild to create field')

        # Getting the Layer feature definition.
        ###feature_def = ogr.OGR_L_GetLayerDefn(layer) 
        feature_def = layer.GetLayerDefn()

        # Loop through queryset creating features
        for item in self.queryset:
            ###feat = ogr.OGR_F_Create(feature_def)
            feat = ogr.Feature(feature_def)

            # For now, set all fields as strings
            # TODO: catch model types and convert to ogr fields
            # http://www.gdal.org/ogr/classOGRFeature.html

            # OGR_F_SetFieldDouble
            #OFTReal => FloatField DecimalField

            # OGR_F_SetFieldInteger
            #OFTInteger => IntegerField

            #OGR_F_SetFieldStrin
            #OFTString => CharField

            # OGR_F_SetFieldDateTime()
            #OFTDateTime => DateTimeField
            #OFTDate => TimeField
            #OFTDate => DateField

            idx = 0
            for field in attributes:
                value = getattr(item,field.name)

                if field.__class__.__name__ == 'FloatField':
                    value = float(value)
                elif field.__class__.__name__ == 'IntegerField':
                    value = int(value)
                else:
                    try:
                        value = str(value)
                    except UnicodeEncodeError, E:
                        # http://trac.osgeo.org/gdal/ticket/882
                        value = ''

                ###ogr.OGR_F_SetFieldString(feat, idx, string_value)
                #changing the following SetField command from accessing field by name to index
                #this change solves an issue that arose sometime after gdal 1.6.3
                #in which the field names became truncated to 10 chars in CreateField
                #feat.SetField(str(field.name),string_value)
                feat.SetField(idx, value)
                idx += 1

            # Transforming & setting the geometry
            geom = getattr(item,geo_field.name)

            # if requested we transform the input geometry
            # to match the shapefiles projection 'to-be'            
            if geom:
                ###ogr_geom = OGRGeometry(geom.wkt,output_srs)
                ogr_geom = ogr.CreateGeometryFromWkt(geom.wkt)
                ###if self.proj_transform:
                ###    ogr_geom.transform(ct)
                # create the geometry
                ###check_err(ogr.OGR_F_SetGeometry(feat, ogr_geom._ptr))
                check_err(feat.SetGeometry(ogr_geom))
            else:
                # Case where geometry object is not found because of null value for field
                # effectively looses whole record in shapefile if geometry does not exist
                pass

            # creat the feature in the layer.
            ###check_err(ogr.OGR_L_SetFeature(layer, feat))
            check_err(layer.CreateFeature(feat))
Ejemplo n.º 24
0
    def write_with_ctypes_bindings(self, tmp_name, queryset, geo_field):
        """ Scrive un file in un formato geografico da un geoqueryset; questa
        funzione usa le librerie Python di GeoDjangos.
        """

        # Get the shapefile driver
        dr = Driver(self.out_format)

        # Creating the datasource
        ds = lgdal.OGR_Dr_CreateDataSource(dr._ptr, tmp_name, None)
        if ds is None:
            raise Exception('Could not create file!')

        # Get the right geometry type number for ogr
        if hasattr(geo_field, 'geom_type'):
            ogr_type = OGRGeomType(geo_field.geom_type).num
        else:
            ogr_type = OGRGeomType(geo_field._geom).num

        # Set up the native spatial reference of geometry field using the srid
        if hasattr(geo_field, 'srid'):
            native_srs = SpatialReference(geo_field.srid)
        else:
            native_srs = SpatialReference(geo_field._srid)

        if self.proj_transform:
            output_srs = SpatialReference(self.proj_transform)
        else:
            output_srs = native_srs

        # create the layer
        # this is crashing python26 on osx and ubuntu
        layer = lgdal.OGR_DS_CreateLayer(ds, 'lyr', output_srs._ptr, ogr_type,
                                         None)

        # Create the fields
        attributes = self.get_attributes()

        for field in attributes:
            fld = lgdal.OGR_Fld_Create(str(field.name), 4)
            added = lgdal.OGR_L_CreateField(layer, fld, 0)
            check_err(added)

        # Getting the Layer feature definition.
        feature_def = lgdal.OGR_L_GetLayerDefn(layer)

        # Loop through queryset creating features
        for item in self.queryset:
            feat = lgdal.OGR_F_Create(feature_def)

            # For now, set all fields as strings
            # TODO: catch model types and convert to ogr fields
            # http://www.gdal.org/ogr/classOGRFeature.html

            # OGR_F_SetFieldDouble
            #OFTReal => FloatField DecimalField

            # OGR_F_SetFieldInteger
            #OFTInteger => IntegerField

            #OGR_F_SetFieldStrin
            #OFTString => CharField

            # OGR_F_SetFieldDateTime()
            #OFTDateTime => DateTimeField
            #OFTDate => TimeField
            #OFTDate => DateField

            for idx, field in enumerate(attributes):
                value = getattr(item, field.name)
                try:
                    string_value = str(value)
                except UnicodeEncodeError, E:
                    # pass for now....
                    # http://trac.osgeo.org/gdal/ticket/882
                    string_value = ''
                lgdal.OGR_F_SetFieldString(feat, idx, string_value)

            # Transforming & setting the geometry
            geom = getattr(item, geo_field.name)

            # if requested we transform the input geometry
            # to match the shapefiles projection 'to-be'
            if geom:
                ogr_geom = OGRGeometry(geom.wkt, output_srs)
                if self.proj_transform:
                    ct = CoordTransform(native_srs, output_srs)
                    ogr_geom.transform(ct)
                # create the geometry
                check_err(lgdal.OGR_F_SetGeometry(feat, ogr_geom._ptr))
            else:
                # Case where geometry object is not found because of null
                # value for field effectively looses whole record in shapefile
                # if geometry does not exist
                pass

            # creat the feature in the layer.
            check_err(lgdal.OGR_L_SetFeature(layer, feat))