예제 #1
0
def editFeature(request, shapefile_id, feature_id=None):
    """ Permite al usuario añadir o editar una feature del shapefile.
        el campo 'feature_id' será None if we are adding a new feature.
    """
    shapefile = Shapefile.objects.get(id=shapefile_id)

    if request.method == "POST" and "delete" in request.POST:
        # User clicked on the "Delete" button -> show "Delete Feature" page.
        return HttpResponseRedirect("/deleteFeature/" + shapefile_id + "/" +
                                    feature_id)

    geometryField = utils.calc_geometry_field(shapefile.geom_type)

    formType = shapefileEditor.getMapForm(shapefile)

    if feature_id == None:
        # Annadiendo una nueva feature.
        feature = Feature(shapefile=shapefile)
        attributes = []
    else:
        # Editando una feature.
        feature = Feature.objects.get(id=feature_id)

    # Toma los atributos para esta feature.

    attributes = []
    for attrValue in feature.attributevalue_set.all():
        attributes.append([attrValue.attribute.name, attrValue.value])
    attributes.sort()

    # Muestra el formulario

    if request.method == "GET":
        wkt = getattr(feature, geometryField)
        form = formType({'geometry': wkt})
        return render_to_response("editFeature.html", {
            'shapefile': shapefile,
            'form': form,
            'attributes': attributes
        })
    elif request.method == "POST":
        form = formType(request.POST)
        try:
            if form.is_valid():
                wkt = form.cleaned_data['geometry']
                setattr(feature, geometryField, wkt)
                feature.save()
                # Devuelve al usuario a la pagina de seleccion de features.
                return HttpResponseRedirect("/edit/" + shapefile_id)
        except ValueError:
            pass

        return render_to_response("editFeature.html", {
            'shapefile': shapefile,
            'form': form,
            'attributes': attributes
        })
예제 #2
0
def edit_feature(request, shapefile_id, feature_id=None):
    if request.method == "POST" and "delete" in request.POST:
        return HttpResponseRedirect("/delete_feature/" + shapefile_id + "/" +
                                    feature_id)
    try:
        shapefile = Shapefile.objects.get(id=shapefile_id)
    except ShapeFile.DoesNotExist:
        return HttpResponseNotFound()

    if feature_id == None:
        feature = Feature(shapefile=shapefile)
    else:
        try:
            feature = Feature.objects.get(id=feature_id)
        except Feature.DoesNotExist:
            return HttpResponseNotFound()
    attributes = []
    for attr_value in feature.attributevalue_set.all():
        attributes.append([attr_value.attribute.name, attr_value.value])
        attributes.sort()
    if request.method == "GET":
        form = MyForm()
        return render(request, "template.html", {'form': form})
    elif request.method == "POST":
        form = MyForm(request.POST)
        if form.is_valid():
            # Extract and save the form's contents here...
            return HttpResponseRedirect("/somewhere/else")
        return render(request, "template.html", {'form': form})
    geometry_field = utils.calc_geometry_field(shapefile.geom_type)
    form_class = utils.get_map_form(shapefile)
    if request.method == "GET":
        wkt = getattr(feature, geometry_field)
        form = form_class({'geometry': wkt})
        return render(request, "edit_feature.html", {
            'shapefile': shapefile,
            'form': form,
            'attributes': attributes
        })
    elif request.method == "POST":
        form = form_class(request.POST)
        try:
            if form.is_valid():
                wkt = form.cleaned_data['geometry']
                setattr(feature, geometry_field, wkt)
                feature.save()
                return HttpResponseRedirect("/edit/" + shapefile_id)
        except ValueError:
            pass
        return render(request, "edit_feature.html", {
            'shapefile': shapefile,
            'form': form,
            'attributes': attributes
        })
예제 #3
0
def edit_feature(request, shapefile_id, feature_id=None):
  if request.method == "POST" and "delete" in request.POST:
    return HttpResponseRedirect("/delete_feature/" +
                                shapefile_id+"/"+feature_id)

  try:
    shapefile = Shapefile.objects.get(id=shapefile_id)
  except ShapeFile.DoesNotExist:
    return HttpResponseNotFound()

  if feature_id == None:
    feature = Feature(shapefile=shapefile)
  else:
    try:
      feature = Feature.objects.get(id=feature_id)
    except Feature.DoesNotExist:
      return HttpResponseNotFound()

  attributes = []
  for attr_value in feature.attributevalue_set.all():
    attributes.append([attr_value.attribute.name,
                       attr_value.value])
  attributes.sort()

  geometry_field = \
       utils.calc_geometry_field(shapefile.geom_type)
  form_class = utils.get_map_form(shapefile)

  if request.method == "GET":
    wkt = getattr(feature, geometry_field)
    form = form_class({'geometry' : wkt})

    return render(request, "edit_feature.html",
                  {'shapefile'  : shapefile,
                   'form'       : form,
                   'attributes' : attributes})
  elif request.method == "POST":
    form = form_class(request.POST)
    try:
      if form.is_valid():
        wkt = form.cleaned_data['geometry']
        setattr(feature, geometry_field, wkt)
        feature.save()
        return HttpResponseRedirect("/edit/" +
                                    shapefile_id)
    except ValueError:
      pass

    return render(request, "edit_feature.html",
                  {'shapefile'  : shapefile,
                   'form'       : form,
                   'attributes' : attributes})
예제 #4
0
def import_data(shapefile):

    # Extract the uploaded shapefile.

    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"]
    has_suffix = {}
    for suffix in required_suffixes:
        has_suffix[suffix] = False

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

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

    shapefile_name = None
    dir_name = tempfile.mkdtemp()
    for info in zip.infolist():
        if info.filename.endswith(".shp"):
            shapefile_name = info.filename

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

    # Open the shapefile.

    try:
        datasource = ogr.Open(os.path.join(dir_name, shapefile_name))
        layer = datasource.GetLayer(0)
        shapefile_ok = True
    except:
        traceback.print_exc()
        shapefile_ok = False

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

    # Save Shapefile object to database.

    src_spatial_ref = layer.GetSpatialRef()
    geom_type = layer.GetLayerDefn().GetGeomType()
    geom_name = ogr.GeometryTypeToName(geom_type)
    shapefile = Shapefile(filename=shapefile_name,
                          srs_wkt=src_spatial_ref.ExportToWkt(),
                          geom_type=geom_name)
    shapefile.save()

    # Define the shapefile's attributes.

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

    # Save the Shapefile's features and attributes to disk.

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

    coord_transform = osr.CoordinateTransformation(src_spatial_ref,
                                                   dst_spatial_ref)

    for i in range(layer.GetFeatureCount()):
        src_feature = layer.GetFeature(i)
        src_geometry = src_feature.GetGeometryRef()
        src_geometry.Transform(coord_transform)
        geometry = GEOSGeometry(src_geometry.ExportToWkt())
        geometry = utils.wrap_geos_geometry(geometry)

        geom_field = utils.calc_geometry_field(geom_name)

        fields = {}
        fields['shapefile'] = shapefile
        fields[geom_field] = geometry

        feature = Feature(**fields)
        feature.save()

        for attr in attributes:
            success, result = utils.get_ogr_feature_attribute(
                attr, src_feature)
            if not success:
                os.remove(fname)
                shutil.rmtree(dir_name)
                shapefile.delete()
                return result

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

    os.remove(fname)
    shutil.rmtree(dir_name)
    return None
예제 #5
0
def import_data(request, shapefile, character_encoding):
    #return "More to come..."
    fd,fname = tempfile.mkstemp(suffix=".zip")

# tempfile.mkstemp() returns both a file descriptor and a filename, we call
# os.close(fd) to close the file descriptor. This allows us to reopen the
# file using open() and write to it in the normal way.
    os.close(fd)

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

# Use the Python standard library's zipfile module to check the
# contents of the uploaded ZIP archive, and return a suitable error message
# if something is wrong

    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"]
    has_suffix = {}
    for suffix in required_suffixes:
        has_suffix[suffix] = False

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

    for suffix in required_suffixes:
        if not has_suffix[suffix]:
            zip.close()
# Delete the temporary file before returning
# an error message, so that we don't leave temporary files lying around
            os.remove(fname)
            return "Archive missing required "+suffix+" file."
# When it's known that the uploaded file is a valid ZIP archive containing
# the files that make up a shapefile, extract these files and store them
# into a temporary directory

    shapefile_name = None
    dst_dir = tempfile.mkdtemp()
    for info in zip.infolist():
        if info.filename.endswith(".shp"):
            shapefile_name = info.filename
        dst_file = os.path.join(dst_dir, info.filename)
        f = open(dst_file, "wb")
        f.write(zip.read(info.filename))
        f.close()
    zip.close()

    
# Open the shapefile
    try:
        datasource = ogr.Open(os.path.join(dst_dir, shapefile_name))
        layer = datasource.GetLayer(0)
        shapefileOK = True
    except:
        traceback.print_exc()
        shapefileOK = False

# if something goes wrong - clean up our temporary files and return a
# suitable error message. 
        if not shapefileOK:
            os.remove(fname)
            shutil.rmtree(dst_dir)
            return "Not a valid shapefile."

# If the shapefile was opened sucessfully - read the data out
# of it. Create the Shapefile object to represent this imported shapefile.
# Get the spatial reference from the shapefile's layer, and then store
# the shapefile's name, spatial reference, and encoding into a Shapefile
# object. The geom_type field is supposed to hold the name of the geometry
# type that this shapefile holds but can't get the name of the geometry
# directly using OGR. Need to implement our own version of
# OGRGeometryTypeToName(). See utils.py module in shared app directory


    src_spatial_ref = layer.GetSpatialRef()
    geometry_type = layer.GetLayerDefn().GetGeomType()
    geometry_name = utils.ogr_type_to_geometry_name(geometry_type)
    curr_user = request.user

    shapefile = Shapefile(filename=shapefile_name,
                          srs_wkt=src_spatial_ref.ExportToWkt(),
                          geom_type=geometry_name,
                          encoding=character_encoding,
                          owner=curr_user)
    shapefile.save()

# Create Attribute objects describing the shapefile's attributes
# Save the Attribute objects into a database and create a
# separate list of these attributes in a variable named attributes.
# Needed for import the attribute values for each feature.

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

# Extract the shapefile's features and store them as Feature objects
# in the database. Because the shapefile's features can be in any spatial
# reference, we need to transform them into our internal spatial reference
# system (EPSG 4326, unprojected latitude, and longitude values) before we
# can store them. To do this, use an OGR CoordinateTransformation() object.
# GEOS geometry object caan be stored into the Feature object.

    dst_spatial_ref = osr.SpatialReference()
    dst_spatial_ref.ImportFromEPSG(4326)
    coord_transform = osr.CoordinateTransformation(src_spatial_ref,
                                     dst_spatial_ref)
    for i in range(layer.GetFeatureCount()):
        src_feature = layer.GetFeature(i)
        # src_name = src_feature.name;
        src_geometry = src_feature.GetGeometryRef()
        src_geometry.Transform(coord_transform)
        geometry = GEOSGeometry(src_geometry.ExportToWkt())
# See wrap geometry in utils: it distinguish polygons and multipoligon,
# linestring and multilinestrings
        geometry = utils.wrap_geos_geometry(geometry)

# Can't simply use the geometry name to identify the field because
# sometimes have to wrap up geometries. See utils.py.

        geometry_field = utils.calc_geometry_field(geometry_name)

# Store the feature's geometry into a Feature object within the database
# Note: Use keyword arguments (**args) to create the Feature object.
# This lets to store the geometry into the correct field of the Feature
# object with a minimum of fuss. 

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

        for attr in attributes:
            success,result = utils.getOGRFeatureAttribute(
                    attr,
                    src_feature,
                    character_encoding)
            if not success:
               os.remove(fname)
               shutil.rmtree(dst_dir)
               shapefile.delete()
               return result

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

    os.remove(fname)
    shutil.rmtree(dst_dir)
    return None
def import_data(shapefile, character_encoding):

    # Save the uploaded file into a temporary file on disk.

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

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

    # Check that the uploaded file is a zip archive.

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

    zip = zipfile.ZipFile(fname)

    # Check that the zip archive contains the required parts of a shapefile.

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

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

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

    # Extract the contents of the zip archive.

    shapefile_name = None
    dst_dir = tempfile.mkdtemp()
    for info in zip.infolist():
        if info.filename.endswith(".shp"):
            shapefile_name = info.filename

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

    zip.close()

    # Open the shapefile.

    try:
        datasource = ogr.Open(os.path.join(dst_dir,
                                           shapefile_name))
        layer = datasource.GetLayer(0)
        shapefile_ok = True
    except:
        traceback.print_exc()
        shapefile_ok = False

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

    # Create our Shapefile object to represent the imported shapefile.

    src_spatial_ref = layer.GetSpatialRef()

    geometry_type = layer.GetLayerDefn().GetGeomType()
    geometry_name = utils.ogr_type_to_geometry_name(geometry_type)

    shapefile = Shapefile(filename=shapefile_name,
                          srs_wkt=src_spatial_ref.ExportToWkt(),
                          geom_type=geometry_name,
                          encoding=character_encoding)
    shapefile.save()

    # Store the shapefile's attribute definitions into the database.

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

    # Set up a coordinate transformation to convert from the shapefile's
    # coordinate system into EPSG 4326 (unprojected lat/long coordinates).

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

    coord_transform = osr.CoordinateTransformation(src_spatial_ref,
                                                   dst_spatial_ref)

    # Process each feature in turn.

    for i in range(layer.GetFeatureCount()):
        src_feature = layer.GetFeature(i)

        # Transform this feature's geometry into EPSG 4326.

        src_geometry = src_feature.GetGeometryRef()
        src_geometry.Transform(coord_transform)
        geometry = GEOSGeometry(src_geometry.ExportToWkt())

        # If necessary, wrap this geometry so that all features have the same
        # geometry type.

        geometry = utils.wrap_geos_geometry(geometry)

        # Calculate the field in the Feature object which will hold the
        # imported feature.

        geometry_field = utils.calc_geometry_field(geometry_name)

        # Create a new Feature object to hold this feature's geometry.

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

        # Store the feature's attribute values into the database.

        for attr in attributes:
            success,result = utils.get_ogr_feature_attribute(
                                      attr, src_feature,
                                      character_encoding)
            if not success:
                os.remove(fname)
                shutil.rmtree(dst_dir)
                shapefile.delete()
                return result

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

    # Finally, clean up.

    os.remove(fname)
    shutil.rmtree(dst_dir)
    return None
예제 #7
0
def import_data(shapefile):
    # Copia el archivo zip en un archivo temporal

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

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

    # Se abre el zip y se chequea su contenido.

    if not zipfile.is_zipfile(fname):
        os.remove(fname)
        return "No es un archivo zip válido."

    zip = zipfile.ZipFile(fname)
    
    #Diferenciacion de los archivos requeridos segun el formato del archivo de entrada

    es_shp = False
    for info in zip.infolist():
        extension = os.path.splitext(info.filename)[1].lower()
        if extension == '.shp':
            es_shp = True
    
    #Sie s un shp, se aplica una comprobación de que todos los archivos necearios estén en el zip
    if es_shp: 
        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 "Archivo extraño: " + info.filename
    
        for suffix in required_suffixes:
            if not hasSuffix[suffix]:
                zip.close()
                os.remove(fname)
                return "No se encuentra el archivo " + suffix + " requerido"
            
    else:
        zip.close()
    
    # Descomprime el zip en un directorio temporal.
    # Se toma el nombre del archivo principal.
        
    zip = zipfile.ZipFile(fname)
    shapefileName = None
    dirname = tempfile.mkdtemp()
    for info in zip.infolist():
        if info.filename.endswith(".shp") and es_shp:
            shapefileName = info.filename
        if not es_shp:
            shapefileName = info.filename
            
        dst_file = os.path.join(dirname, info.filename)
        f = open(dst_file, "wb")
        f.write(zip.read(info.filename))
        f.close()
    zip.close()

    # Intenta abrir el archivo

    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 "No es un archivo válido."

    # Importar los datos del shapefile abierto.
    
    feature1 = layer.GetFeature(0)
    tipo_geometria = feature1.geometry().GetGeometryType()
    feature1.Destroy()
    #geometryType  = layer.GetLayerDefn().GetGeomType()
    geometryName = ogr.GeometryTypeToName(tipo_geometria)
    geometryName = geometryName.replace(" ", "")
    src_spatial_ref = layer.GetSpatialRef()
    dst_spatial_ref = osr.SpatialReference()
    dst_spatial_ref.ImportFromEPSG(4326)         #Aqui se define que sistema de coordenadas se le asigna en la BD

    shapefile = Shapefile(filename=shapefileName,
                          srs_wkt=src_spatial_ref.ExportToWkt(),
                          geom_type=geometryName)
    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)
    #layerDef.Destroy()    
    
    coordTransform = osr.CoordinateTransformation(src_spatial_ref,
                                                  dst_spatial_ref)
        
    for i in range(layer.GetFeatureCount()):
        srcFeature = layer.GetFeature(i)
        srcGeometry = srcFeature.GetGeometryRef()
        srcGeometry.Transform(coordTransform)
        geometry = GEOSGeometry(srcGeometry.ExportToWkt())
        geometry = utils.wrap_geos_geometry(geometry)
        geometryField = utils.calc_geometry_field(geometryName)
        args = {}
        args['shapefile'] = shapefile
        args[geometryField] = geometry
        feature = Feature(**args)
        feature.save()
        
        for attr in attributes:
            success,result = utils.get_ogr_feature_attribute(attr, srcFeature)
            #if not success:
                #os.remove(fname)
                #print dirname
                #shutil.rmtree(dirname)
                #shapefile.delete()
                #return result
            if success:
                attrValue = AttributeValue(feature=feature, attribute=attr, value=result)
                attrValue.save()
        
    # Finalmente, limpiarlo todo.
    datasource.Destroy()
    os.remove(fname)
    print dirname
    shutil.rmtree(dirname)

    return None # Exito.
예제 #8
0
def edit_feature(request, shapefile_id, feature_id=None, attributevalue=None):
    userid = request.user
    if request.method == "POST" and "delete" in request.POST:
      return HttpResponseRedirect("/editor/delete_feature/" +
                                  shapefile_id+"/"+feature_id)

    try:
      shapefile = Shapefile.objects.get(id=shapefile_id)
    except ShapeFile.DoesNotExist:
      return HttpResponseNotFound()
      
      
    if request.method == "POST" and "attributevalue" in request.POST:
      print attributevalue	    
      return HttpResponseRedirect("/editor/edit_attributevalue/" +
                                  shapefile_id+"/"+attributevalue)

    try:
      shapefile = Shapefile.objects.get(id=shapefile_id)
    except ShapeFile.DoesNotExist:
      return HttpResponseNotFound()
      
      
    if request.method == "POST" and "addattributevalue" in request.POST:
      print feature_id	    
      return HttpResponseRedirect("/editor/add_attributevalue/" +
                                  str(shapefile_id)+"/"+str(feature_id))

    try:
      shapefile = Shapefile.objects.get(id=shapefile_id)
    except ShapeFile.DoesNotExist:
      return HttpResponseNotFound()
      
      
      
# Create a new Feature object if the feature_id is not specified,
# but fail if an invalid feature ID was specified.

    if feature_id == None:
      feature = Feature(shapefile=shapefile)
           
    else:
      try:
        feature = Feature.objects.get(id=feature_id)
        
      except Feature.DoesNotExist:
        return HttpResponseNotFound()

# Store attributes and attributes' values in an array
    attributes = []
    for attr_value in feature.attributevalue_set.all():
      attributes.append([attr_value.attribute.name,
                         attr_value.value,
                         attr_value.id])
    attributes.sort()



   # get all attributes for shapefile
    q = []
    q = Feature.objects.raw('SELECT * from shared_attribute where shapefile_id=%s', [shapefile_id])
    all_attr = []
    for p in q:
        all_attr.append(p.id)
            
  # get only attributes that have values
    q1 = []
    set_attr = []
    q1 = Feature.objects.raw('SELECT * from shared_attributevalue where feature_id=%s', [feature_id])
    for p in q1:
        set_attr.append(p.attribute_id) 	  
  
    attributes1 = []
    attr1 = []
    for n in all_attr:
        	if n not in set_attr:
                  #print n
                  attr = Attribute.objects.get(id=n)
                  attr1.append(attr.name)
                  attr1.append(attr.id)
                  attributes1.append(attr1)
                  attr1=[]
    attributes1.sort()  
    print attributes1
 
    geometry_field = \
            utils.calc_geometry_field(shapefile.geom_type)
    form_class = utils.get_map_form(shapefile)

    if request.method == "GET":
      wkt = getattr(feature, geometry_field)
      form = form_class({'geometry' : wkt})

      return render(request, "edit_feature.html",
                  {'shapefile' : shapefile,
                   'form' : form,
                   'feature': feature_id,
                   'attributes' : attributes,
                   'available': attributes1,
		   'full_name': request.user.username})
      
    elif request.method == "POST":
      form = form_class(request.POST)
      try:
        if form.is_valid():
          wkt = form.cleaned_data['geometry']
          setattr(feature, geometry_field, wkt)
          feature.save()

          return HttpResponseRedirect("/editor/edit_feature/" +
			    str(shapefile_id)+"/"+str(feature.id))
      except ValueError:
        pass

    return render(request, "edit_feature.html",
                  {'shapefile' : shapefile,
                   'form' : form,
                   'feature': feature_id,
                   'attributes' : attributes,
                   'available': attributes1,
		   'full_name': request.user.username})
def import_data(shapefile):

    # Extract the uploaded shapefile.

    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"]
    has_suffix = {}
    for suffix in required_suffixes:
        has_suffix[suffix] = False

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

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

    shapefile_name = None
    dir_name = tempfile.mkdtemp()
    for info in zip.infolist():
        if info.filename.endswith(".shp"):
            shapefile_name = info.filename

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

    # Open the shapefile.

    try:
        datasource = ogr.Open(os.path.join(dir_name, shapefile_name))
        layer      = datasource.GetLayer(0)
        shapefile_ok = True
    except:
        traceback.print_exc()
        shapefile_ok = False

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

    # Save Shapefile object to database.

    src_spatial_ref = layer.GetSpatialRef()
    geom_type = layer.GetLayerDefn().GetGeomType()
    geom_name = ogr.GeometryTypeToName(geom_type)
    shapefile = Shapefile(filename=shapefile_name,
                          srs_wkt=src_spatial_ref.ExportToWkt(),
                          geom_type=geom_name)
    shapefile.save()

    # Define the shapefile's attributes.

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

    # Save the Shapefile's features and attributes to disk.

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

    coord_transform = osr.CoordinateTransformation(
                                      src_spatial_ref,
                                      dst_spatial_ref)

    for i in range(layer.GetFeatureCount()):
        src_feature = layer.GetFeature(i)
        src_geometry = src_feature.GetGeometryRef()
        src_geometry.Transform(coord_transform)
        geometry = GEOSGeometry(src_geometry.ExportToWkt())
        geometry = utils.wrap_geos_geometry(geometry)

        geom_field = utils.calc_geometry_field(geom_name)

        fields = {}
        fields['shapefile'] = shapefile
        fields[geom_field] = geometry

        feature = Feature(**fields)
        feature.save()

        for attr in attributes:
            success,result = utils.get_ogr_feature_attribute(
                                      attr, src_feature)
            if not success:
                os.remove(fname)
                shutil.rmtree(dir_name)
                shapefile.delete()
                return result

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

    os.remove(fname)
    shutil.rmtree(dir_name)
    return None