Example #1
0
def importData(file, characterEncoding, format, user, folder):
    start_time = time.time()
    try:
        #manage zipfile
        fd, fname = tempfile.mkstemp(suffix=fileExt_dic[format])
        os.close(fd)
        f = open(fname, "wb")
        for chunk in file.chunks():
            f.write(chunk)
        f.close()

        if not zipfile.is_zipfile(fname):
            os.remove(fname)
            return "Not a valid zip archive.", None
        zip = zipfile.ZipFile(fname)

        hasSuffix = {}
        required_suffixes = suffixes_dic[format]
        for suffix in required_suffixes:
            hasSuffix[suffix] = False
        for info in zip.infolist():
            extension = os.path.splitext(info.filename)[1].lower()
            if extension in required_suffixes:
                hasSuffix[extension] = True
        for suffix in required_suffixes:
            if not hasSuffix[suffix]:
                zip.close()
                os.remove(fname)
                return "Archive missing required %s file." % suffix, None

        zip = zipfile.ZipFile(fname)
        shapefileName = None
        dirname = tempfile.mkdtemp()
        for info in zip.infolist():
            if info.filename.endswith(filenameExt_dic[format]):
                shapefileName = info.filename
            dstFile = os.path.join(dirname, info.filename)
            f = open(dstFile, "wb")
            f.write(zip.read(info.filename))
            f.close()
        zip.close()

        #verify if shapefile is valid
        driver = ogr.GetDriverByName(ogrDriver_dic[format])
        datasource = driver.Open(os.path.join(dirname, shapefileName))

        for i in range(datasource.GetLayerCount()):
            layer = datasource.GetLayer(i)
            srcSpatialRef = layer.GetSpatialRef()
            geometryType = layer.GetGeomType()
            geometryName = utils.ogrTypeToGeometryName(geometryType)
            print "geometry type:" + str(geometryType), geometryName
            srs_wkt = srcSpatialRef.ExportToWkt()
            shapefile = Shapefile.objects.create(name=layer.GetName(),
                                                 parent=folder,
                                                 srs_wkt=srs_wkt,
                                                 geom_type=geometryName,
                                                 encoding=characterEncoding,
                                                 created_by=user)

            #define shapefile's attributes
            attributes = []
            layerDef = layer.GetLayerDefn()
            for i in range(layerDef.GetFieldCount()):
                fieldDef = layerDef.GetFieldDefn(i)
                attr = Attribute(shapefile=shapefile,
                                 name=fieldDef.GetName(),
                                 type=fieldDef.GetType(),
                                 width=fieldDef.GetWidth(),
                                 precision=fieldDef.GetPrecision())
                attributes.append(attr)
            Attribute.objects.bulk_create(attributes)

            #store shapefile's features
            dstSpatialRef = osr.SpatialReference()
            dstSpatialRef.ImportFromEPSG(3857)
            coordTransform = osr.CoordinateTransformation(
                srcSpatialRef, dstSpatialRef)

            features = []
            for i in range(layer.GetFeatureCount()):
                srcFeature = layer.GetNextFeature()
                id = srcFeature.GetFID()
                srcGeometry = srcFeature.GetGeometryRef()
                srcGeometry.Transform(coordTransform)
                if srcGeometry.GetCoordinateDimension() == 3:
                    srcGeometry
                #geometry = GEOSGeometry(srcGeometry.ExportToWkt())
                #geometry = utils.wrapGEOSGeometry(geometry)
                #geometryField = utils.calcGeometryField(geometryName)
                multi_geom = ogr.ForceToMultiPolygon(srcGeometry)

                #Store shapefile"s attributes
                hash_attributeValue = defaultdict(dict)
                attribute_value = {}
                attributes.sort(key=lambda x: x.name.lower())
                for attr in attributes:
                    success, result = utils.getOGRFeatureAttribute(
                        attr, srcFeature, characterEncoding)
                    if not success:
                        datasource.Destroy()
                        os.remove(fname)
                        shutil.rmtree(dirname)
                        shapefile.delete()
                        return 'Wrong character encoding', None
                    hash_attributeValue.setdefault(attr.name, result)

                feature = Feature(shapefile=shapefile,
                                  attribute_value=hash_attributeValue,
                                  id_relat=id)
                setattr(feature, 'geom_multipolygon', multi_geom.ExportToWkt())
                features.append(feature)

            Feature.objects.bulk_create(features)

        print("Temps final: --- %s seconds ---" %
              str(time.time() - start_time))
        return None, shapefile

    except Exception, e:
        return e, None
Example #2
0
def importData (shapefile, characterEncoding ):

    fname = None

    # store a copy of the UploadedFile so it can be manipulated
    fd, fname = tempfile.mkstemp (suffix=".zip")
    os.close (fd)
    f = open (fname , "wb")
    for chunk in shapefile.chunks ():
        f.write (chunk)
    f.close ()

    # ----------

    if not zipfile.is_zipfile (fname):
      os.remove (fname)
      return "Not a valid zip archive %s " %(fname)

    # check that the shapefile zip has the required files
    zip = zipfile.ZipFile (fname)

    required_suffixes = [ ".shp" , ".shx" , ".dbf" , ".prj" ]
    hasSuffix = {}

    for suffix in required_suffixes:
      hasSuffix [suffix] = False

    for info in zip.infolist ():
      extn = os.path.splitext (info.filename) [1].lower ()
      if (extn in required_suffixes): 
        hasSuffix [extn] = True
 
    for suffix in required_suffixes:
      if not hasSuffix [suffix]:
        zip.close ()
        os.remove ( fname )
        return "Archive missing required "+suffix+"  file."

    # save the shapefile components (shp, dff etc) into
    # a temporary directory

    zip = zipfile.ZipFile (fname)
    shapefileName = None
    dirname = tempfile.mkdtemp ()
    for info in zip.infolist ():
        if info.filename.endswith (".shp"):
            shapefileName = info.filename
        dstFile = os.path.join ( dirname, info.filename )
        f = open (dstFile, "wb")
        f.write (zip.read ( info.filename ) )
        f.close ()
    zip.close ()

    # Use OGR to (try to) open up the Shapefile
    try:
        datasource= ogr.Open (os.path.join ( dirname ,  shapefileName ) ) 
        layer = datasource.GetLayer (0)
        shapeFileOK = True
    except:   
        traceback.print_exc()
        shapeFileOK = False
 
    if not  shapeFileOK:
        os.remove (fname)
        shutil.rmtree (dirname)
        return "Not a valid shapefile... couldn't open it with OGR"

    # Add the shapefile to the database
    srcSpatialRef = layer.GetSpatialRef ()

    geometryType = layer.GetLayerDefn ().GetGeomType ()
    geometryName = utils.ogrTypeToGeometryName (geometryType) 

    print "about to save shapefile. The details: " 
    print ".. srs_skt " + srcSpatialRef.ExportToWkt ()
    print ".. shapefileName: " + shapefileName


    # TODO. This will be truncated and should not be...
    srs_wkd = srcSpatialRef.ExportToWkt () [0:254]

    shapefile     = Shapefile ( filename = shapefileName , 
                                srs_wkd  = srs_wkd, 
                                geom_type= geometryName, 
                                encoding = characterEncoding)

    shapefile.save ()

    # Add the shapefile's attributes to the database

    attributes = []
    layerDef = layer.GetLayerDefn ()

    for counter in range ( layerDef.GetFieldCount () ):
        fieldDef = layerDef.GetFieldDefn ( counter ) 
        attr     = Attribute  (  shapefile = shapefile , 
                                 name      = fieldDef.GetName () , 
                                 type      = fieldDef.GetType () , # int code
                                 precision = fieldDef.GetPrecision () ,  
                                 width     = fieldDef.GetWidth () 
                              )

        attr.save ()
        attributes.append (attr)

    # Import the features
  
    dstSpatialRef = osr.SpatialReference () 
    dstSpatialRef.ImportFromEPSG ( 4326 ) 
    
    coordTransform  = osr.CoordinateTransformation ( srcSpatialRef , dstSpatialRef 	)    

    for count in range ( layer.GetFeatureCount () ):

        srcFeature   = layer.GetFeature ( count ) 
        srcGeometry  = srcFeature.GetGeometryRef () 
        srcGeometry.Transform ( coordTransform ) 

        # return a limited set of geometries .. 
        geometry = utils.wrapGEOSGeometry ( srcGeometry ) 

        # Geometry field will correspond with the field name in Feature class / table
        geometryField = utils.calcGeometryField (geometryName) \
                                                 # param is e.g. LineString / Point

        args = {}
        args[ 'shapefile'] = shapefile
        args[ geometryField] = geometry
        feature = Feature ( **args )       
          
        feature.save ()

        # save the attribute values
        for attr in attributes:

            # srcFeature (ogr.Layer.Feature) 
            # attr (shapeEditor.models.Attribute) 
            success, result = utils.getOGRFeatureAttribute (
                                  attr, srcFeature,
                                  characterEncoding ) 

            if not success:
                os.remove (fname)
                shutil.rmtree (dirname)
                shapefile.delete ()
                return result

            value = result
            attrValue = AttributeValue ( feature = feature, 
                                         attribute = attr , 
                                         value = value ) 
            attrValue.save ()

    os.remove (fname)
    shutil.rmtree (dirname)
    return None
Example #3
0
def importData(shapefile, characterEncoding):
    fd, fname = tempfile.mkstemp(suffix=".zip")
    os.close(fd)
    f = open(fname, "wb")
    for chunk in shapefile.chunks():
        f.write(chunk)
    f.close()

    if not zipfile.is_zipfile(fname):
        os.remove(fname)
        return "Not a valid zip archive."

    zip = zipfile.ZipFile(fname)
    required_suffixes = [".shp", ".shx", ".dbf", ".prj"]
    hasSuffix = {}
    for suffix in required_suffixes:
        hasSuffix[suffix] = False
    for info in zip.infolist():
        extension = os.path.splitext(info.filename)[1].lower()
        if extension in required_suffixes:
            hasSuffix[extension] = True
    for suffix in required_suffixes:
        if not hasSuffix[suffix]:
            zip.close()
            os.remove(fname)
            return "Archive missing required " + suffix + " file."
    zip = zipfile.ZipFile(fname)
    shapefileName = None
    dirname = tempfile.mkdtemp()
    for info in zip.infolist():
        if info.filename.endswith(".shp"):
            shapefileName = info.filename
        dstFile = os.path.join(dirname, info.filename)
        f = open(dstFile, "wb")
        f.write(zip.read(info.filename))
        f.close()
    zip.close()

    try:
        datasource = ogr.Open(os.path.join(dirname, shapefileName))
        layer = datasource.GetLayer(0)
        shapefileOK = True
    except:
        traceback.print_exc()
        shapefileOK = False
        if not shapefileOK:
            os.remove(fname)
            shutil.rmtree(dirname)
            return "Not a valid shapefile."

    srcSpatialRef = layer.GetSpatialRef()
    geometryType = layer.GetLayerDefn().GetGeomType()
    geometryName = utils.ogrTypeToGeometryName(geometryType)
    shapefile = Shapefile(filename=shapefileName,
                          srs_wkt=srcSpatialRef.ExportToWkt(),
                          geom_type=geometryName,
                          encoding=characterEncoding)
    shapefile.save()

    attributes = []
    layerDef = layer.GetLayerDefn()
    for i in range(layerDef.GetFieldCount()):
        fieldDef = layerDef.GetFieldDefn(i)
        attr = Attribute(shapefile=shapefile,
                         name=fieldDef.GetName(),
                         type=fieldDef.GetType(),
                         width=fieldDef.GetWidth(),
                         precision=fieldDef.GetPrecision())
        attr.save()
        attributes.append(attr)

    dstSpatialRef = osr.SpatialReference()
    dstSpatialRef.ImportFromEPSG(4326)
    coordTransform = osr.CoordinateTransformation(srcSpatialRef, dstSpatialRef)
    for i in range(layer.GetFeatureCount()):
        srcFeature = layer.GetFeature(i)
        srcGeometry = srcFeature.GetGeometryRef()
        srcGeometry.Transform(coordTransform)
        geometry = GEOSGeometry(srcGeometry.ExportToWkt())
        geometry = utils.wrapGEOSGeometry(geometry)
        geometryField = utils.calcGeometryField(geometryName)
        args = {}
        args['shapefile'] = shapefile
        args[geometryField] = geometry
        feature = Feature(**args)
        feature.save()

    for attr in attributes:
        success, result = utils.getOGRFeatureAttribute(attr, srcFeature,
                                                       characterEncoding)
        if not success:
            os.remove(fname)
            shutil.rmtree(dirname)
            shapefile.delete()
            return result
        attrValue = AttributeValue(feature=feature,
                                   attribute=attr,
                                   value=result)
        attrValue.save()

    os.remove(fname)
    shutil.rmtree(dirname)

    return "Data imported!"
Example #4
0
def importexc(request):
    if request.method == "GET":
        form = ImportExcForm()
        return render_to_response("importexc.html",
                                  {'form' : form})
    elif request.method == "POST":

        form = ImportExcForm(request.POST,
                             request.FILES)
        if form.is_valid():
            excfile = request.FILES['import_exc']
            character_encoding = request.POST['character_encoding']
            excel_file = xlrd.open_workbook(file_contents=excfile.read())
            filename=excel_file.sheet_names()
            filename = filename[0]
            dirpath = tempfile.mkdtemp()
            sh = excel_file.sheet_by_index(0)
            w = shapefile.Writer(shapefile.POINT)

            w.field('Station','I')
            w.field('Longitude', 'F')
            w.field('Latitude', 'F')
            w.field('Gravel_pc', 'F')
            w.field('Sand_pc', 'F')
            w.field('Mud_pc', 'F')

            for rownum in range(sh.nrows):
                if rownum == 0:
                    continue
                else:
                    x_coord = sh.cell_value(rowx=rownum, colx=1)
                    y_coord = sh.cell_value(rowx=rownum, colx=2)

                    w.point(x_coord, y_coord)

                    w.record(Station=sh.cell_value(rowx=rownum, colx=0),Latitude=sh.cell_value(rowx=rownum, colx=2),
                             Longitude=sh.cell_value(rowx=rownum, colx=1),Gravel_pc=sh.cell_value(rowx=rownum, colx=3),
                             Sand_pc=sh.cell_value(rowx=rownum, colx=4),Mud_pc=sh.cell_value(rowx=rownum, colx=5))

            w.save(os.path.join(dirpath,filename))

            prj = open("%s.prj" % os.path.join(dirpath,filename), "w")
            epsg = 'GEOGCS["WGS 84",DATUM["WGS_1984",SHEROID["WGS84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]'
            prj.write(epsg)
            prj.close()

            for item in os.listdir(dirpath):
                if item.endswith(".shp"):
                    shapefileName = item
                    datasource = ogr.Open(os.path.join(dirpath, shapefileName))
                    layer = datasource.GetLayer(0)
                    layerDefinition = layer.GetLayerDefn()
                    srcSpatialRef = layer.GetSpatialRef()
                    geometryType = layer.GetLayerDefn().GetGeomType()
                    geometryName = utils.ogrTypeToGeometryName(geometryType)

                    shpfile = Shpfile(
                        filename=shapefileName,
                        srs_wkt=srcSpatialRef.ExportToWkt(),
                        geom_type=geometryName,
                        encoding=character_encoding
                    )

                    shpfile.save()

                    attributes = []
                    layerDef = layer.GetLayerDefn()
                    for i in range(layerDef.GetFieldCount()):
                        fieldDef = layerDef.GetFieldDefn(i)
                        attr = Attribute(
                            shpfile=shpfile,
                            name=fieldDef.GetName(),
                            type=fieldDef.GetType(),
                            width=fieldDef.GetWidth(),
                        )
                        attr.save()
                        attributes.append(attr)

                    for i in range(layer.GetFeatureCount()):
                        srcFeature = layer.GetFeature(i)
                        srcGeometry = srcFeature.GetGeometryRef()
                        geometry = GEOSGeometry(srcGeometry.ExportToWkt())
                        geometry = utils.wrapGEOSGeometry(geometry)
                        geometryField = utils.calcGeometryField(geometryName)
                        args = {}
                        args['shpfile'] = shpfile
                        args[geometryField] = geometry
                        feature = Feature(**args)
                        feature.save()

                    for attr in attributes:
                        success,result = utils.getOGRFeatureAttribute(
                            attr,
                            srcFeature,
                            character_encoding
                        )
                        if not success:
                            shutil.rmtree(dirpath)
                            shpfile.delete()
                            return result

                        attrValue = AttributeValue(
                            feature=feature,
                            attribute=attr,
                            value=result
                        )
                        attrValue.save()



            shutil.rmtree(dirpath)



    return HttpResponse("data imported!!")
Example #5
0
def importData(file, characterEncoding, format, user, folder):
    start_time = time.time()
    try:
        #manage zipfile
        fd,fname = tempfile.mkstemp(suffix=fileExt_dic[format])
        os.close(fd)
        f = open(fname, "wb")
        for chunk in file.chunks():
            f.write(chunk)
        f.close()

        if not zipfile.is_zipfile(fname):
            os.remove(fname)
            return "Not a valid zip archive.", None
        zip = zipfile.ZipFile(fname)

        hasSuffix = {}
        required_suffixes = suffixes_dic[format]
        for suffix in required_suffixes:
            hasSuffix[suffix] = False
        for info in zip.infolist():
            extension = os.path.splitext(info.filename)[1].lower()
            if extension in required_suffixes:
                hasSuffix[extension] = True
        for suffix in required_suffixes:
            if not hasSuffix[suffix]:
                zip.close()
                os.remove(fname)
                return "Archive missing required %s file." % suffix, None

        zip = zipfile.ZipFile(fname)
        shapefileName = None
        dirname = tempfile.mkdtemp()
        for info in zip.infolist():
            if info.filename.endswith(filenameExt_dic[format]):
                shapefileName = info.filename
            dstFile = os.path.join(dirname, info.filename)
            f = open(dstFile, "wb")
            f.write(zip.read(info.filename))
            f.close()
        zip.close()

        #verify if shapefile is valid
        driver = ogr.GetDriverByName(ogrDriver_dic[format])
        datasource = driver.Open(os.path.join(dirname,shapefileName))

        for i in range(datasource.GetLayerCount()):
            layer = datasource.GetLayer(i)
            srcSpatialRef = layer.GetSpatialRef()
            geometryType = layer.GetGeomType()
            geometryName = utils.ogrTypeToGeometryName(geometryType)
            print "geometry type:" + str(geometryType), geometryName
            srs_wkt=srcSpatialRef.ExportToWkt()
            shapefile = Shapefile.objects.create(name=layer.GetName(), parent=folder, srs_wkt=srs_wkt, geom_type=geometryName, encoding=characterEncoding, created_by=user)

            #define shapefile's attributes
            attributes = []
            layerDef = layer.GetLayerDefn()
            for i in range(layerDef.GetFieldCount()):
                fieldDef = layerDef.GetFieldDefn(i)
                attr = Attribute(shapefile=shapefile, name=fieldDef.GetName(), type=fieldDef.GetType(), width=fieldDef.GetWidth(), precision=fieldDef.GetPrecision())
                attributes.append(attr)
            Attribute.objects.bulk_create(attributes)

            #store shapefile's features
            dstSpatialRef = osr.SpatialReference()
            dstSpatialRef.ImportFromEPSG(3857)
            coordTransform = osr.CoordinateTransformation(srcSpatialRef, dstSpatialRef)

            features = []
            for i in range(layer.GetFeatureCount()):
                srcFeature = layer.GetNextFeature()
                id = srcFeature.GetFID()
                srcGeometry = srcFeature.GetGeometryRef()
                srcGeometry.Transform(coordTransform)
                if srcGeometry.GetCoordinateDimension() == 3:
                    srcGeometry
                #geometry = GEOSGeometry(srcGeometry.ExportToWkt())
                #geometry = utils.wrapGEOSGeometry(geometry)
                #geometryField = utils.calcGeometryField(geometryName)
                multi_geom = ogr.ForceToMultiPolygon(srcGeometry)

                #Store shapefile"s attributes
                hash_attributeValue = defaultdict(dict)
                attribute_value = {}
                attributes.sort(key=lambda x:x.name.lower())
                for attr in attributes:
                    success,result = utils.getOGRFeatureAttribute(attr, srcFeature, characterEncoding)
                    if not success:
                        datasource.Destroy()
                        os.remove(fname)
                        shutil.rmtree(dirname)
                        shapefile.delete()
                        return 'Wrong character encoding', None
                    hash_attributeValue.setdefault(attr.name,result)

                feature = Feature(shapefile=shapefile, attribute_value=hash_attributeValue, id_relat=id)
                setattr(feature, 'geom_multipolygon', multi_geom.ExportToWkt() )
                features.append(feature)

            Feature.objects.bulk_create(features)

        print("Temps final: --- %s seconds ---" % str(time.time() - start_time))
        return None, shapefile

    except Exception, e:
        return e, None
def importData(shapefile, characterEncoding):
    fd,fname = tempfile.mkstemp(suffix=".zip")
    os.close(fd)
    f = open(fname, "wb")
    for chunk in shapefile.chunks():
        f.write(chunk)
    f.close()

    if not zipfile.is_zipfile(fname):
        os.remove(fname)
        return "Not a valid zip archive."

    zip = zipfile.ZipFile(fname)
    required_suffixes = [".shp", ".shx", ".dbf", ".prj"]
    hasSuffix = {}
    for suffix in required_suffixes:
        hasSuffix[suffix] = False
    for info in zip.infolist():
        extension = os.path.splitext(info.filename)[1].lower()
        if extension in required_suffixes:
            hasSuffix[extension] = True
    for suffix in required_suffixes:
        if not hasSuffix[suffix]:
            zip.close()
            os.remove(fname)
            return "Archive missing required "+suffix+" file."
    zip = zipfile.ZipFile(fname)
    shapefileName = None
    dirname = tempfile.mkdtemp()
    for info in zip.infolist():
        if info.filename.endswith(".shp"):
            shapefileName = info.filename
        dstFile = os.path.join(dirname, info.filename)
        f = open(dstFile, "wb")
        f.write(zip.read(info.filename))
        f.close()
    zip.close()

    try:
        datasource = ogr.Open(os.path.join(dirname, shapefileName))
        layer = datasource.GetLayer(0)
        shapefileOK = True
    except:
        traceback.print_exc()
        shapefileOK = False
        if not shapefileOK:
            os.remove(fname)
            shutil.rmtree(dirname)
            return "Not a valid shapefile."

    srcSpatialRef = layer.GetSpatialRef()
    geometryType = layer.GetLayerDefn().GetGeomType()
    geometryName = utils.ogrTypeToGeometryName(geometryType)
    shapefile = Shapefile(
            filename=shapefileName,
            srs_wkt=srcSpatialRef.ExportToWkt(),
            geom_type=geometryName,
            encoding=characterEncoding
            )
    shapefile.save()

    attributes = []
    layerDef = layer.GetLayerDefn()
    for i in range(layerDef.GetFieldCount()):
        fieldDef = layerDef.GetFieldDefn(i)
        attr = Attribute(
                shapefile=shapefile,
                name=fieldDef.GetName(),
                type=fieldDef.GetType(),
                width=fieldDef.GetWidth(),
                precision=fieldDef.GetPrecision()
                )
        attr.save()
        attributes.append(attr)

    dstSpatialRef = osr.SpatialReference()
    dstSpatialRef.ImportFromEPSG(4326)
    coordTransform = osr.CoordinateTransformation(srcSpatialRef,
            dstSpatialRef)
    for i in range(layer.GetFeatureCount()):
        srcFeature = layer.GetFeature(i)
        srcGeometry = srcFeature.GetGeometryRef()
        srcGeometry.Transform(coordTransform)
        geometry = GEOSGeometry(srcGeometry.ExportToWkt())
        geometry = utils.wrapGEOSGeometry(geometry)
        geometryField = utils.calcGeometryField(geometryName)
        args = {}
        args['shapefile'] = shapefile
        args[geometryField] = geometry
        feature = Feature(**args)
        feature.save()

    for attr in attributes:
        success,result = utils.getOGRFeatureAttribute(
                attr,
                srcFeature,
                characterEncoding
                )
        if not success:
            os.remove(fname)
            shutil.rmtree(dirname)
            shapefile.delete()
            return result
        attrValue = AttributeValue(
                feature=feature,
                attribute=attr,
                value=result
                )
        attrValue.save()

    os.remove(fname)
    shutil.rmtree(dirname)

    return "Data imported!"
Example #7
0
def importData(shapefile, characterEncoding):

    fname = None

    # store a copy of the UploadedFile so it can be manipulated
    fd, fname = tempfile.mkstemp(suffix=".zip")
    os.close(fd)
    f = open(fname, "wb")
    for chunk in shapefile.chunks():
        f.write(chunk)
    f.close()

    # ----------

    if not zipfile.is_zipfile(fname):
        os.remove(fname)
        return "Not a valid zip archive %s " % (fname)

    # check that the shapefile zip has the required files
    zip = zipfile.ZipFile(fname)

    required_suffixes = [".shp", ".shx", ".dbf", ".prj"]
    hasSuffix = {}

    for suffix in required_suffixes:
        hasSuffix[suffix] = False

    for info in zip.infolist():
        extn = os.path.splitext(info.filename)[1].lower()
        if (extn in required_suffixes):
            hasSuffix[extn] = True

    for suffix in required_suffixes:
        if not hasSuffix[suffix]:
            zip.close()
            os.remove(fname)
            return "Archive missing required " + suffix + "  file."

    # save the shapefile components (shp, dff etc) into
    # a temporary directory

    zip = zipfile.ZipFile(fname)
    shapefileName = None
    dirname = tempfile.mkdtemp()
    for info in zip.infolist():
        if info.filename.endswith(".shp"):
            shapefileName = info.filename
        dstFile = os.path.join(dirname, info.filename)
        f = open(dstFile, "wb")
        f.write(zip.read(info.filename))
        f.close()
    zip.close()

    # Use OGR to (try to) open up the Shapefile
    try:
        datasource = ogr.Open(os.path.join(dirname, shapefileName))
        layer = datasource.GetLayer(0)
        shapeFileOK = True
    except:
        traceback.print_exc()
        shapeFileOK = False

    if not shapeFileOK:
        os.remove(fname)
        shutil.rmtree(dirname)
        return "Not a valid shapefile... couldn't open it with OGR"

    # Add the shapefile to the database
    srcSpatialRef = layer.GetSpatialRef()

    geometryType = layer.GetLayerDefn().GetGeomType()
    geometryName = utils.ogrTypeToGeometryName(geometryType)

    print "about to save shapefile. The details: "
    print ".. srs_skt " + srcSpatialRef.ExportToWkt()
    print ".. shapefileName: " + shapefileName

    # TODO. This will be truncated and should not be...
    srs_wkd = srcSpatialRef.ExportToWkt()[0:254]

    shapefile = Shapefile(filename=shapefileName,
                          srs_wkd=srs_wkd,
                          geom_type=geometryName,
                          encoding=characterEncoding)

    shapefile.save()

    # Add the shapefile's attributes to the database

    attributes = []
    layerDef = layer.GetLayerDefn()

    for counter in range(layerDef.GetFieldCount()):
        fieldDef = layerDef.GetFieldDefn(counter)
        attr = Attribute(
            shapefile=shapefile,
            name=fieldDef.GetName(),
            type=fieldDef.GetType(),  # int code
            precision=fieldDef.GetPrecision(),
            width=fieldDef.GetWidth())

        attr.save()
        attributes.append(attr)

    # Import the features

    dstSpatialRef = osr.SpatialReference()
    dstSpatialRef.ImportFromEPSG(4326)

    coordTransform = osr.CoordinateTransformation(srcSpatialRef, dstSpatialRef)

    for count in range(layer.GetFeatureCount()):

        srcFeature = layer.GetFeature(count)
        srcGeometry = srcFeature.GetGeometryRef()
        srcGeometry.Transform(coordTransform)

        # return a limited set of geometries ..
        geometry = utils.wrapGEOSGeometry(srcGeometry)

        # Geometry field will correspond with the field name in Feature class / table
        geometryField = utils.calcGeometryField (geometryName) \
                                                 # param is e.g. LineString / Point

        args = {}
        args['shapefile'] = shapefile
        args[geometryField] = geometry
        feature = Feature(**args)

        feature.save()

        # save the attribute values
        for attr in attributes:

            # srcFeature (ogr.Layer.Feature)
            # attr (shapeEditor.models.Attribute)
            success, result = utils.getOGRFeatureAttribute(
                attr, srcFeature, characterEncoding)

            if not success:
                os.remove(fname)
                shutil.rmtree(dirname)
                shapefile.delete()
                return result

            value = result
            attrValue = AttributeValue(feature=feature,
                                       attribute=attr,
                                       value=value)
            attrValue.save()

    os.remove(fname)
    shutil.rmtree(dirname)
    return None
Example #8
0
def importData(shapefile, characterEncoding):
    """ Attempt to import the contents of a shapefile into our database.

        'shapefile' is the Django UploadedFile object that was uploaded, and
        'characterEncoding' is the character encoding to use for interpreting
        the shapefile's string attributes.

        We return None if the import succeeded.  Otherwise we return a string
        containing a suitable error message explaining why the shapefile can't
        be imported.
    """
    # Copy the zip archive into a temporary file.

    fd,fname = tempfile.mkstemp(suffix=".zip")
    os.close(fd)

    f = open(fname, "wb")
    for chunk in shapefile.chunks():
        f.write(chunk)
    f.close()

    # Open the zip file and check its contents.

    if not zipfile.is_zipfile(fname):
        os.remove(fname)
        return "Not a valid zip archive."

    zip = zipfile.ZipFile(fname)

    required_suffixes = [".shp", ".shx", ".dbf", ".prj"]
    hasSuffix = {}
    for suffix in required_suffixes:
        hasSuffix[suffix] = False

    for info in zip.infolist():
        extension = os.path.splitext(info.filename)[1].lower()
        if extension in required_suffixes:
            hasSuffix[extension] = True
        else:
            print "Extraneous file: " + info.filename

    for suffix in required_suffixes:
        if not hasSuffix[suffix]:
            zip.close()
            os.remove(fname)
            return "Archive missing required " + suffix + " file."

    # Decompress the zip archive into a temporary directory.  At the same
    # time, we get the name of the main ".shp" file.

    zip = zipfile.ZipFile(fname)
    shapefileName = None
    dirname = tempfile.mkdtemp()
    for info in zip.infolist():
        if info.filename.endswith(".shp"):
            shapefileName = info.filename

        dstFile = os.path.join(dirname, info.filename)
        f = open(dstFile, "wb")
        f.write(zip.read(info.filename))
        f.close()
    zip.close()

    # Attempt to open the shapefile.

    try:
        datasource  = ogr.Open(os.path.join(dirname, shapefileName))
        layer       = datasource.GetLayer(0)
        shapefileOK = True
    except:
        traceback.print_exc()
        shapefileOK = False

    if not shapefileOK:
        os.remove(fname)
        shutil.rmtree(dirname)
        return "Not a valid shapefile."

    # Import the data from the opened shapefile.

    geometryType  = layer.GetLayerDefn().GetGeomType()
    geometryName  = utils.ogrTypeToGeometryName(geometryType)
    srcSpatialRef = layer.GetSpatialRef()
    dstSpatialRef = osr.SpatialReference()
    dstSpatialRef.ImportFromEPSG(4326)

    shapefile = Shapefile(filename=shapefileName,
                          srs_wkt=srcSpatialRef.ExportToWkt(),
                          geom_type=geometryName,
                          encoding=characterEncoding)
    shapefile.save()

    attributes = []
    layerDef = layer.GetLayerDefn()
    for i in range(layerDef.GetFieldCount()):
        fieldDef = layerDef.GetFieldDefn(i)
        attr = Attribute(shapefile=shapefile,
                         name=fieldDef.GetName(),
                         type=fieldDef.GetType(),
                         width=fieldDef.GetWidth(),
                         precision=fieldDef.GetPrecision())
        attr.save()
        attributes.append(attr)

    coordTransform = osr.CoordinateTransformation(srcSpatialRef,
                                                  dstSpatialRef)

    for i in range(layer.GetFeatureCount()):
        srcFeature = layer.GetFeature(i)
        srcGeometry = srcFeature.GetGeometryRef()
        srcGeometry.Transform(coordTransform)
        geometry = GEOSGeometry(srcGeometry.ExportToWkt())
        geometry = utils.wrapGEOSGeometry(geometry)
        geometryField = utils.calcGeometryField(geometryName)
        args = {}
        args['shapefile'] = shapefile
        args[geometryField] = geometry
        feature = Feature(**args)
        feature.save()

        for attr in attributes:
            success,result = \
                    utils.getOGRFeatureAttribute(attr, srcFeature,
                                                 characterEncoding)
            if not success:
                os.remove(fname)
                shutil.rmtree(dirname)
                shapefile.delete()
                return result

            attrValue = AttributeValue(feature=feature,
                                       attribute=attr,
                                       value=result)
            attrValue.save()

    # Finally, clean everything up.

    os.remove(fname)
    shutil.rmtree(dirname)

    return None # success.