Example #1
0
 def get_fieldsets(self, request, obj=None):
     fieldsets = copy.deepcopy(super(FeatureAdmin, self).get_fieldsets(request, obj))
     if not obj:
         return fieldsets
     else:
         fieldsets[0][1]['fields'].insert(2, calcGeometryField(obj.shapefile.geom_type))
         return fieldsets
Example #2
0
def editFeature(request, shapefile_id, feature_id=None):
    """ Let the user add or edit a feature within the given shapefile.

        'feature_id' will be 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("/shape-editor/deleteFeature/" +
                                    shapefile_id + "/" + feature_id)

    geometryField = utils.calcGeometryField(shapefile.geom_type)

    formType = shapefileEditor.getMapForm(shapefile)

    if feature_id == None:
        # Adding a new feature.
        feature = Feature(shapefile=shapefile)
        attributes = []
    else:
        # Editing an existing feature.
        feature = Feature.objects.get(id=feature_id)

    # Get the attributes for this feature.

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

    # Display the form.

    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()
                # Return the user to the "select feature" page.
                return HttpResponseRedirect("/shape-editor/edit/" +
                                            shapefile_id)
        except ValueError:
            pass

        return render_to_response("editFeature.html",
                                  {'shapefile'  : shapefile,
                                   'form'       : form,
                                   'attributes' : attributes})
Example #3
0
 def get_fieldsets(self, request, obj=None):
     fieldsets = copy.deepcopy(
         super(FeatureAdmin, self).get_fieldsets(request, obj))
     if not obj:
         return fieldsets
     else:
         fieldsets[0][1]['fields'].insert(
             2, calcGeometryField(obj.shapefile.geom_type))
         return fieldsets
Example #4
0
def editFeature(request, shapefile_id, feature_id=None):

    if request.method == "POST" and "delete" in request.POST:
        return HttpResponseRedirect("/shape-editor/deleteFeature/" +
                                    shapefile_id + "/" + feature_id)

    try:
        shapefile = Shapefile.objects.get(id=shapefile_id)
    except:
        raise Http404

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

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

    geometryField = utils.calcGeometryField(shapefile.geom_type)
    formType = utils.getMapForm(shapefile)

    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()
                return HttpResponseRedirect("/shape-editor/edit/" +
                                            str(shapefile.id))

        except ValueError:
            pass
        return render_to_response("editFeature.html", {
            'shapefile': shapefile,
            'form': form,
            'attributes': attributes
        })
def exportData(shapefile):
    dstDir = tempfile.mkdtemp()
    dstFile = str(os.path.join(dstDir, shapefile.filename))
    dstSpatialRef = osr.SpatialReference()
    dstSpatialRef.ImportFromWkt(shapefile.srs_wkt)
    driver = ogr.GetDriverByName("ESRI Shapefile")
    datasource = driver.CreateDataSource(dstFile)
    layer = datasource.CreateLayer(str(shapefile.filename),
            dstSpatialRef)
    for attr in shapefile.attribute_set.all():
        field = ogr.FieldDefn(str(attr.name), attr.type)
        field.SetWidth(attr.width)
        field.SetPrecision(attr.precision)
        layer.CreateField(field)

    srcSpatialRef = osr.SpatialReference()
    srcSpatialRef.ImportFromEPSG(4326)
    coordTransform = osr.CoordinateTransformation(srcSpatialRef,
            dstSpatialRef)
    geomField = utils.calcGeometryField(shapefile.geom_type)
    for feature in shapefile.feature_set.all():
        geometry = getattr(feature, geomField)
        geometry = utils.unwrapGEOSGeometry(geometry)
        dstGeometry = ogr.CreateGeometryFromWkt(geometry.wkt)
        dstGeometry.Transform(coordTransform)
        dstFeature = ogr.Feature(layer.GetLayerDefn())
        dstFeature.SetGeometry(dstGeometry)
        for attrValue in feature.attributevalue_set.all():
            utils.setOGRFeatureAttribute(
                    attrValue.attribute,
                    attrValue.value,
                    dstFeature,
                    shapefile.encoding)
            layer.CreateFeature(dstFeature)
        dstFeature.Destroy()
    datasource.Destroy()
    temp = tempfile.TemporaryFile()
    zip = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
    shapefileBase = os.path.splitext(dstFile)[0]
    shapefileName = os.path.splitext(shapefile.filename)[0]
    for fName in os.listdir(dstDir):
        zip.write(os.path.join(dstDir, fName), fName)
    zip.close()
    shutil.rmtree(dstDir)

    f = FileWrapper(temp)
    response = HttpResponse(f, content_type="application/zip")
    response['Content-Disposition'] = \
            "attachment; filename=" + shapefileName + ".zip"
    response['Content-Length'] = temp.tell()
    temp.seek(0)

    return response
Example #6
0
def editFeature (request, shapefile_id, feature_id=None) :

    if request.method == "POST" and "delete" in request.POST:
        return HttpResponseRedirect ("/shape-editor/deleteFeature/" + shapefile_id + "/" + feature_id)

    try:
        shapefile = Shapefile.objects.get (id = shapefile_id)
    except:
        raise Http404

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

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

    geometryField = utils.calcGeometryField ( shapefile.geom_type ) 
    formType      = utils.getMapForm (shapefile)

    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 () 
                return HttpResponseRedirect ("/shape-editor/edit/" + str (shapefile.id))

        except ValueError:
            pass
        return render_to_response ("editFeature.html",
                                   { 'shapefile' : shapefile, 
                                     'form'      : form, 
                                     'attributes': attributes
                                   } )
Example #7
0
def exportData(shapefile):
    dstDir = tempfile.mkdtemp()
    dstFile = str(os.path.join(dstDir, shapefile.filename))
    dstSpatialRef = osr.SpatialReference()
    dstSpatialRef.ImportFromWkt(shapefile.srs_wkt)
    driver = ogr.GetDriverByName("ESRI Shapefile")
    datasource = driver.CreateDataSource(dstFile)
    layer = datasource.CreateLayer(str(shapefile.filename), dstSpatialRef)
    for attr in shapefile.attribute_set.all():
        field = ogr.FieldDefn(str(attr.name), attr.type)
        field.SetWidth(attr.width)
        field.SetPrecision(attr.precision)
        layer.CreateField(field)

    srcSpatialRef = osr.SpatialReference()
    srcSpatialRef.ImportFromEPSG(4326)
    coordTransform = osr.CoordinateTransformation(srcSpatialRef, dstSpatialRef)
    geomField = utils.calcGeometryField(shapefile.geom_type)
    for feature in shapefile.feature_set.all():
        geometry = getattr(feature, geomField)
        geometry = utils.unwrapGEOSGeometry(geometry)
        dstGeometry = ogr.CreateGeometryFromWkt(geometry.wkt)
        dstGeometry.Transform(coordTransform)
        dstFeature = ogr.Feature(layer.GetLayerDefn())
        dstFeature.SetGeometry(dstGeometry)
        for attrValue in feature.attributevalue_set.all():
            utils.setOGRFeatureAttribute(attrValue.attribute, attrValue.value,
                                         dstFeature, shapefile.encoding)
            layer.CreateFeature(dstFeature)
        dstFeature.Destroy()
    datasource.Destroy()
    temp = tempfile.TemporaryFile()
    zip = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
    shapefileBase = os.path.splitext(dstFile)[0]
    shapefileName = os.path.splitext(shapefile.filename)[0]
    for fName in os.listdir(dstDir):
        zip.write(os.path.join(dstDir, fName), fName)
    zip.close()
    shutil.rmtree(dstDir)

    f = FileWrapper(temp)
    response = HttpResponse(f, content_type="application/zip")
    response['Content-Disposition'] = \
            "attachment; filename=" + shapefileName + ".zip"
    response['Content-Length'] = temp.tell()
    temp.seek(0)

    return response
Example #8
0
def getMapForm(shapefile):
    """ Return a form.Form subclass for editing the given shapefile's features.

        The form will have a single field, 'geometry', which lets the user edit
        the feature's geometry.
    """
    # Setup a dummy admin instance to auto-generate our map widget.

    geometryField = utils.calcGeometryField(shapefile.geom_type)
    geometryType  = utils.calcGeometryFieldType(shapefile.geom_type)

    if geometryType == "Point":
        adminType = PointAdmin
    elif geometryType == "LineString":
        adminType = LineStringAdmin
    elif geometryType == "Polygon":
        adminType = PolygonAdmin
    elif geometryType == "MultiPoint":
        adminType = MultiPointAdmin
    elif geometryType == "MultiLineString":
        adminType = MultiLineStringAdmin
    elif geometryType == "MultiPolygon":
        adminType = MultiPolygonAdmin
    elif geometryType == "GeometryCollection":
        adminType = GeometryCollectionAdmin

    adminInstance = adminType(Feature, admin.site)
    field  = Feature._meta.get_field(geometryField)

    widgetType = adminInstance.get_map_widget(field)

    # Define a form which encapsulates the desired editing field.

    class MapForm(forms.Form):
        geometry = forms.CharField(widget=widgetType(),
                                   label="")

    return MapForm
Example #9
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)

        dirname = tempfile.mkdtemp()
        for info in zip.infolist():
            if info.filename.endswith(filenameExt_dic[format]):
                filename = 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 vectorfile is valid
        ds = DataSource(os.path.join(dirname, filename),
                        encoding=characterEncoding)

        for srcLayer in ds:
            layer = Shapefile.objects.create(name=srcLayer.name,
                                             parent=folder,
                                             srs_wkt=srcLayer.srs,
                                             geom_type=srcLayer.geom_type.name,
                                             encoding=characterEncoding,
                                             created_by=user)

            #define layer's attributes
            attributes = []
            for idx in range(srcLayer.num_fields):
                attr = Attribute(shapefile=layer,
                                 name=srcLayer.fields[idx],
                                 type=srcLayer.field_types[idx].__name__,
                                 width=srcLayer.field_widths[idx],
                                 precision=srcLayer.field_precisions[idx])
                attributes.append(attr)
            Attribute.objects.bulk_create(attributes)

            #store layer's features
            srcSpatialRef = SpatialReference(srcLayer.srs.wkt)
            dstSpatialRef = SpatialReference('EPSG:3857')
            ct = CoordTransform(srcSpatialRef, dstSpatialRef)

            features = []
            for srcFeature in srcLayer:
                srcGeometry = srcFeature.geom
                srcGeometry.transform(ct)
                srcGeometry = srcGeometry.geos
                ##                if srcGeometry.coord_dim > 2:
                ##                    srcGeometry = srcGeometry.flatten2D()
                srcGeometry = utils.wrapGEOSGeometry(srcGeometry)

                #Store layer"s attributes
                hash_attributeValue = {}
                attribute_value = {}
                attributes.sort(key=lambda x: x.name.lower())
                for attr in attributes:
                    try:
                        value = srcFeature.get(attr.name)
                    except DjangoUnicodeDecodeError:
                        return "Wrong character encoding", None

                    if type(value) == datetime.date:
                        value = value.isoformat()
                    hash_attributeValue[attr.name] = value

                feature = Feature(shapefile=layer,
                                  attribute_value=hash_attributeValue,
                                  id_relat=srcFeature.fid)
                setattr(feature, utils.calcGeometryField(srcLayer.geom_type),
                        srcGeometry)
                features.append(feature)

            Feature.objects.bulk_create(features)

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

    except Exception, e:
        return e, None
Example #10
0
def exportMergedData(layers, encoding, EPSG, format):
    start_time = time.time()
    try:
        dstSpatialRef = osr.SpatialReference()
        dstSpatialRef.ImportFromEPSG(EPSG)
        driver = ogr.GetDriverByName(ogrDriver_dic[format])
        layer_name = '_'.join([layer.name for layer in layers]) + "_merged"
        attributes = [
            attribute for layer in layers
            for attribute in layer.attribute_set.all()
        ]
        features = tuple([
            (feature, layer.geom_type) for layer in layers
            for feature in layer.feature_set.all().order_by('id_relat')
        ])

        if format == "shp":
            if len({g for f, g in features}) != 1:
                return "Shapefile format does not support different geometry types in the same layer", None

        dstDir = tempfile.mkdtemp()
        dstFile = str(
            os.path.join(dstDir, layer_name + filenameExt_dic[format]))
        datasource = driver.CreateDataSource(dstFile)
        vlayer = datasource.CreateLayer(str(layer.name), dstSpatialRef)

        #retrive attributes
        for attr in attributes:
            field = ogr.FieldDefn(str(attr.name), attr.type)
            field.SetWidth(attr.width)
            field.SetPrecision(attr.precision)
            fields.append(field)
            vlayer.CreateField(field)

    #save features in shapefile
        srcSpatialRef = osr.SpatialReference()
        srcSpatialRef.ImportFromEPSG(3857)
        coordTransform = osr.CoordinateTransformation(srcSpatialRef,
                                                      dstSpatialRef)
        for feature, geom_type in features:
            geometry = getattr(feature, utils.calcGeometryField(geom_type))

            if geometry:
                geometry = utils.unwrapGEOSGeometry(geometry)
                dstGeometry = ogr.CreateGeometryFromWkt(geometry.wkt)
                dstGeometry.Transform(coordTransform)
            else:
                dstGeometry = None

        #save attributes in the shapefile
            dstFeature = ogr.Feature(vlayer.GetLayerDefn())
            dstFeature.SetGeometry(dstGeometry)
            for attrName, attrValue in feature.attribute_value.iteritems():
                attribute = Attribute.objects.get(name=str(attrName),
                                                  shapefile=feature.shapefile)
                utils.setOGRFeatureAttribute(attribute, attrValue, dstFeature,
                                             encoding)
            vlayer.CreateFeature(dstFeature)
            dstFeature.Destroy()
        datasource.Destroy()

        #compress the shapefile
        temp = tempfile.TemporaryFile()
        zip = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
        shapefileBase = os.path.splitext(dstFile)[0]
        shapefileName = os.path.splitext(layer_name)[0]
        for fName in os.listdir(dstDir):
            zip.write(os.path.join(dstDir, fName), fName)

        zip.close()

        #delete temporary files
        shutil.rmtree(dstDir)

        #return the zip to user
        f = FileWrapper(temp)
        response = StreamingHttpResponse(f, content_type="application/zip")
        response[
            'Content-Disposition'] = "attachment; filename=" + shapefileName + fileExt_dic[
                format]
        response['Content-Length'] = temp.tell()
        temp.seek(0)
        print("Temps final: --- %s seconds ---" %
              str(time.time() - start_time))
        return None, response
    except BaseException, e:
        return e, None
Example #11
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)

        dirname = tempfile.mkdtemp()
        for info in zip.infolist():
            if info.filename.endswith(filenameExt_dic[format]):
                filename = 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 vectorfile is valid
        ds = DataSource(os.path.join(dirname,filename), encoding=characterEncoding)

        for srcLayer in ds:
            layer = Shapefile.objects.create(name=srcLayer.name, parent=folder, srs_wkt=srcLayer.srs, geom_type=srcLayer.geom_type.name, encoding=characterEncoding, created_by=user)

            #define layer's attributes
            attributes = []
            for idx in range(srcLayer.num_fields):
                attr = Attribute(shapefile=layer, name=srcLayer.fields[idx], type=srcLayer.field_types[idx].__name__, width=srcLayer.field_widths[idx], precision=srcLayer.field_precisions[idx])
                attributes.append(attr)
            Attribute.objects.bulk_create(attributes)

            #store layer's features
            srcSpatialRef = SpatialReference(srcLayer.srs.wkt)
            dstSpatialRef = SpatialReference('EPSG:3857')
            ct = CoordTransform(srcSpatialRef,dstSpatialRef)

            features = []
            for srcFeature in srcLayer :
                srcGeometry = srcFeature.geom
                srcGeometry.transform(ct)
                srcGeometry = srcGeometry.geos
            ##                if srcGeometry.coord_dim > 2:
            ##                    srcGeometry = srcGeometry.flatten2D()
                srcGeometry =  utils.wrapGEOSGeometry(srcGeometry)



                #Store layer"s attributes
                hash_attributeValue = {}
                attribute_value = {}
                attributes.sort(key=lambda x:x.name.lower())
                for attr in attributes:
                    try:
                        value = srcFeature.get(attr.name)
                    except DjangoUnicodeDecodeError:
                        return "Wrong character encoding", None

                    if type(value) == datetime.date:
                        value = value.isoformat()
                    hash_attributeValue[attr.name] = value

                feature = Feature(shapefile=layer, attribute_value=hash_attributeValue, id_relat=srcFeature.fid)
                setattr(feature, utils.calcGeometryField(srcLayer.geom_type), srcGeometry)
                features.append(feature)

            Feature.objects.bulk_create(features)

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

    except Exception, e:
        return e, None
Example #12
0
def exportMergedData(layers, encoding, EPSG, format):
    start_time = time.time()
    try:
        dstSpatialRef = osr.SpatialReference()
        dstSpatialRef.ImportFromEPSG(EPSG)
        driver = ogr.GetDriverByName(ogrDriver_dic[format])
        layer_name = '_'.join([layer.name for layer in layers])+"_merged"
        attributes = [attribute for layer in layers for attribute in layer.attribute_set.all()]
        features = tuple([(feature, layer.geom_type) for layer in layers for feature in layer.feature_set.all().order_by('id_relat')])

        if format == "shp":
            if len({g for f, g in features}) != 1:
                return "Shapefile format does not support different geometry types in the same layer", None

        dstDir = tempfile.mkdtemp()
        dstFile = str(os.path.join(dstDir, layer_name+filenameExt_dic[format]))
        datasource = driver.CreateDataSource(dstFile)
        vlayer = datasource.CreateLayer(str(layer.name),dstSpatialRef)

        #retrive attributes
        for attr in attributes:
            field = ogr.FieldDefn(str(attr.name), attr.type)
            field.SetWidth(attr.width)
            field.SetPrecision(attr.precision)
            fields.append(field)
            vlayer.CreateField(field)

    #save features in shapefile
        srcSpatialRef = osr.SpatialReference()
        srcSpatialRef.ImportFromEPSG(3857)
        coordTransform = osr.CoordinateTransformation(srcSpatialRef, dstSpatialRef)
        for feature, geom_type in features:
            geometry = getattr(feature, utils.calcGeometryField(geom_type))

            if geometry:
                geometry = utils.unwrapGEOSGeometry(geometry)
                dstGeometry = ogr.CreateGeometryFromWkt(geometry.wkt)
                dstGeometry.Transform(coordTransform)
            else:
                dstGeometry = None

        #save attributes in the shapefile
            dstFeature = ogr.Feature(vlayer.GetLayerDefn())
            dstFeature.SetGeometry(dstGeometry)
            for attrName, attrValue in feature.attribute_value.iteritems():
                attribute = Attribute.objects.get(name=str(attrName), shapefile=feature.shapefile)
                utils.setOGRFeatureAttribute(attribute, attrValue, dstFeature, encoding)
            vlayer.CreateFeature(dstFeature)
            dstFeature.Destroy()
        datasource.Destroy()

        #compress the shapefile
        temp = tempfile.TemporaryFile()
        zip = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
        shapefileBase = os.path.splitext(dstFile)[0]
        shapefileName = os.path.splitext(layer_name)[0]
        for fName in os.listdir(dstDir):
            zip.write(os.path.join(dstDir, fName), fName)

        zip.close()

        #delete temporary files
        shutil.rmtree(dstDir)

        #return the zip to user
        f = FileWrapper(temp)
        response = StreamingHttpResponse(f, content_type="application/zip")
        response['Content-Disposition'] = "attachment; filename=" + shapefileName + fileExt_dic[format]
        response['Content-Length'] = temp.tell()
        temp.seek(0)
        print("Temps final: --- %s seconds ---" % str(time.time() - start_time))
        return None, response
    except BaseException, e:
        return e, None
Example #13
0
def xml_map_config(request, zoom, x, y, basqui_map, layersMapOptions):
    basqui_map.changed = False
    basqui_map.save()
    map = mapnik.Map(
        TILE_WIDTH, TILE_HEIGHT,
        "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over"
    )

    for layer in layersMapOptions.all():
        try:
            layerLabel = LayerLabel.objects.get(layerMapOptions=layer)
        except ObjectDoesNotExist:
            layerLabel = None
        for layerStyle in layer.styles.all():
            shapefile = layer.layer
            geometryField = utils.calcGeometryField(shapefile.geom_type)
            if layerLabel:
                query = '(select ' + geometryField + ', attribute_value->\'' + layerLabel.field.name + '\' as label, id_relat from "shapefile_feature" where' + ' shapefile_id in (' + str(
                    shapefile.id) + ')) as geom'
            else:
                query = '(select ' + geometryField + ', id_relat from "shapefile_feature" where' + ' shapefile_id in (' + str(
                    shapefile.id) + ')) as geom'

            datasource = mapnik.PostGIS(user=dbSettings['USER'],
                                        password=dbSettings['PASSWORD'],
                                        dbname=dbSettings['NAME'],
                                        port=5433,
                                        table=query,
                                        srid=3857,
                                        geometry_field=geometryField,
                                        geometry_table='"shapefile_feature"')

            featureLayer = mapnik.Layer(
                str(shapefile.filename) + str(layerStyle.id))
            featureLayer.srs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over"
            featureLayer.datasource = datasource
            featureLayer.styles.append(
                str(shapefile.filename) + '_Style' + str(layerStyle.id))

            #defining the feature layer styles
            style_rule = mapnik.Rule()
            if shapefile.geom_type in ["Point", "MultiPoint"]:
                s = mapnik.PointSymbolizer(
                    mapnik.PathExpression(str(layerStyle.iconName)))
                s.allow_overlap = True
                style_rule.symbols.append(s)
            elif shapefile.geom_type in ["LineString", "MultiLineString"]:
                style_rule.symbols.append(
                    mapnik.LineSymbolizer(
                        mapnik.Color(str(layerStyle.strokeColor)),
                        layerStyle.strokeWeight))
            elif shapefile.geom_type in ["Polygon", "MultiPolygon"]:
                p = mapnik.PolygonSymbolizer(
                    mapnik.Color(str((layerStyle.fillColor))))
                p.fill_opacity = layerStyle.fillOpacity
                style_rule.symbols.append(p)
                style_rule.symbols.append(
                    mapnik.LineSymbolizer(
                        mapnik.Color(str(layerStyle.strokeColor)),
                        layerStyle.strokeWeight))
            layer_style = mapnik.Style()
            layer_style.rules.append(style_rule)

            #defining label styles
            if layerLabel:
                label_rule = mapnik.Rule()
                label = mapnik.TextSymbolizer(
                    mapnik.Expression('[label]'), 'DejaVu Sans Book',
                    layerLabel.font_size,
                    mapnik.Color(str(layerLabel.font_color)))
                label.halo_fill = mapnik.Color(str(layerLabel.halo_color))
                label.halo_radius = int(layerLabel.halo_radius)
                #label.label_placement = mapnik.label_placement.INTERIOR_PLACEMENT
                label.allow_overlap = False
                label.avoid_edges = True
                label_rule.symbols.append(label)
                label_style = mapnik.Style()
                label_style.rules.append(label_rule)
                featureLayer.styles.append(
                    str(shapefile.filename) + '_label' + str(layerStyle.id))
                #add label to the map
                map.append_style(
                    str(shapefile.filename) + '_label' + str(layerStyle.id),
                    label_style)

            #add new feature to the map
            map.append_style(
                str(shapefile.filename) + '_Style' + str(layerStyle.id),
                layer_style)
            map.layers.append(featureLayer)

    #saving the map mapnik xml
    mapnik.save_map(
        map, "c:\\basqui\\tilestache\\%s\\%s.xml" %
        (str(request.user), str(basqui_map.map_name)))
Example #14
0
def exportData (shapefile):

  dstDir  = tempfile.mkdtemp ()
  dstFile = str ( os.path.join (dstDir, shapefile.filename) ) 

  dstSpatialRef = osr.SpatialReference ()
  dstSpatialRef.ImportFromWkt ( shapefile.srs_wkd)

  driver     = ogr.GetDriverByName ( "ESRI Shapefile")
  datasource = driver.CreateDataSource ( dstFile )
  layer      = datasource.CreateLayer (  str ( shapefile.filename ), dstSpatialRef )   

  # find the spatial reference
  srcSpatialRef = osr.SpatialReference ()
  srcSpatialRef.ImportFromEPSG ( 4326 )
  coordTransform = osr.CoordinateTransformation ( srcSpatialRef , dstSpatialRef) 
  
  geomField = utils.calcGeometryField ( shapefile.geom_type ) 

  # Define layer attributes
  for attr in shapefile.attribute_set.all ():
    field = ogr.FieldDefn ( str ( attr.name ) , attr.type ) 
    field.SetWidth ( attr.width )
    field.SetPrecision ( attr.precision ) 
    layer.CreateField ( field ) 

  # Add all the (Single type) geometries for this layer 
  for feature in shapefile.feature_set.all ():

    # Geometry is a django construct inherited from GeometryField

    geometry = getattr ( feature , geomField ) 
    geometry = utils.unwrapGEOSGeometry  (geometry)

    dstGeometry = ogr.CreateGeometryFromWkt ( geometry.wkt ) 
    dstGeometry.Transform ( coordTransform ) 

    dstFeature = ogr.Feature ( layer.GetLayerDefn () ) 
    dstFeature.SetGeometry ( dstGeometry )

    # add in the feature's attributes
    for attrValue in feature.attributevalue_set.all ():
      utils.setOGRFeatureAttribute ( attrValue.attribute , 
                                     attrValue.value, 
                                     dstFeature, 
                                     shapefile.encoding)

    layer.CreateFeature ( dstFeature ) 
    dstFeature.Destroy ()

  datasource.Destroy ()
   
  # Compress the shapefile
  temp          = tempfile.TemporaryFile () 
  zipHandle      = zipfile.ZipFile ( temp, 'w' , zipfile.ZIP_DEFLATED ) 
  
  for fName in os.listdir ( dstDir ):
    zipHandle.write ( os.path.join ( dstDir , fName ) , fName ) 
  zipHandle.close ()

  # useful links to temp's directory
#  shapefileBase = os.path.splitext ( dstFile) [0]
  shapefileName = os.path.splitext ( shapefile.filename)[0]

  # Delete the temporary files
  shutil.rmtree ( dstDir ) 

  # Return the zip archive to the user
  f= FileWrapper ( temp ) 
  response = HttpResponse ( f , content_type="application\zip")
  response ['Content-Disposition'] = \
     "attachment; filename=" + shapefileName + ".zip"
  response ['Content-Length'] = temp.tell ()
  temp.seek (0)
  return response
Example #15
0
def tile (request, version, shapefile_id, zoom, x, y):

  shapefile = None
  try:

    if version != "1.0":
      raise Http404
    try:
      shapefile = Shapefile.objects.get (id=shapefile_id)
    except Shapefile.DoesNotExist:
      raise Http404

    zoom = int (zoom)
    x    = int (x)
    y    = int (y)

    # min(x) = 0, max (x) =

    if zoom < 0 or zoom > MAX_ZOOM_LEVEL:
      raise Http404

    # for TILE_WIDTH/HEIGHT==256
    # at zoom = 0 extents will be 180 units/degrees

    xExtent  = _unitsPerPixel (zoom) * TILE_WIDTH
    yExtent  = _unitsPerPixel (zoom) * TILE_HEIGHT

    minLong  = xExtent * x - 180.0
    minLat   = yExtent * y -  90.0

    maxLong  = minLong + xExtent
    maxLat   = minLat  + xExtent    

    if ( minLong < -180 or maxLong > 180  or
        minLat < -90 or maxLat > 90):
         print "bound error raised"
         raise Http404

    dbFile        = "/home/john360/computing/geospatial_dev/geodjango_shapefile_app/DB/geodjango.db"
    extentStr = "%s,%s,%s,%s" %(minLong, minLat, maxLong, maxLat)

    map = mapnik.Map (TILE_WIDTH, TILE_HEIGHT, 
                      '+proj=longlat +datum=WGS84 +no_defs') 
 
    map.background = mapnik.Color ("#7391ad")


#    time.sleep (0.3)


    # Set up the base layer 
    datasource = \
             mapnik.SQLite(file=dbFile,
             table="shapeEditor_baseMap",
             key_field="id",
#             srid=4326,
             geometry_field="geometry",
             extent=extentStr,
             wkb_format="spatialite")

    baseLayer = mapnik.Layer ("baseLayer")      
    baseLayer.datasource = datasource
    baseLayer.styles.append ("baseLayerStyle")

    rule = mapnik.Rule ()

    rule.symbols.append ( 
      mapnik.PolygonSymbolizer (mapnik.Color ("#b5d19c") )
    )

    rule.symbols.append ( 
      mapnik.LineSymbolizer (mapnik.Color ("#404040"), 0.2 )
    )

    style = mapnik.Style ()
    style.rules.append ( rule ) 

    map.append_style ("baseLayerStyle", style)
    map.layers.append (baseLayer)

    # Define the feature layer

    geometryField = utils.calcGeometryField ( shapefile.geom_type)
    
    query = '( select ' + geometryField  +  \
            ' from "shapeEditor_feature" where ' + \
            'shapefile_id = ' + str(shapefile.id) + ' ) as geom'  

    dbSettings = settings.DATABASES['default']

    datasource = \
             mapnik.PostGIS(user=dbSettings['USER'],
                            password=dbSettings['PASSWORD'],
                            dbname=dbSettings['NAME'],
                            table=query,
                            srid=4326,
                            geometry_field=geometryField,
                            geometry_table='"shapeEditor_feature"')

    featureLayer = mapnik.Layer ("featureLayer")
    featureLayer.datasource = datasource
    featureLayer.styles.append ("featureLayerStyle")    

    rule = mapnik.Rule ()
	
    if shapefile.geom_type in ["Point", "MultiPoint"]:
      rule.symbols.append ( mapnik.LineSymbolizer (mapnik.Color ("#000000"), 0.5) )

    elif shapefile.geom_type in ["Polygon", "Multipolygon"]:

      rule.symbols.append ( mapnik.PolygonSymbolizer (mapnik.Color ("#b5d19c") ) )
      rule.symbols.append ( mapnik.LineSymbolizer (mapnik.Color ("#000000"), 1) )

    style = mapnik.Style () 
    style.rules.append (rule)

    map.append_style ("featureLayerStyle", style)
    map.layers.append (featureLayer)

    map.zoom_to_box (mapnik.Envelope (minLong, minLat, maxLong, maxLat ))
    image = mapnik.Image ( TILE_WIDTH, TILE_HEIGHT)

    mapnik.render (map, image)
    imageData = image.tostring ('png')

    return HttpResponse (imageData, mimetype="image/png")

  except:
    traceback.print_exc ()
    return HttpResponse (" ")
Example #16
0
def tileLayer(request, version, shapefile_id, zoom, x, y):
    try:
        if version != "1.0":
            raise Http404
        try:
            shapefile = Shapefile.objects.get(id=shapefile_id, created_by=request.user)
        except Shapefile.DoesNotExist:
            raise Http404
        zoom = int(zoom)
        x = int(x)
        y = int(y)
        if zoom < 0 or zoom > MAX_ZOOM_LEVEL:
            raise Http404
        xExtent = _unitsPerPixel(zoom)
        yExtent = _unitsPerPixel(zoom)
        minLong = x * xExtent - 20037508.34
        minLat = y * yExtent - 20037508.34
        maxLong = minLong + xExtent
        maxLat = minLat + yExtent
        if (minLong < -20037508.34 or maxLong > 20037508.34 or minLat < -20037508.34 or maxLat > 20037508.34):
            raise Http404

        #create de mapnik.map object
        map = mapnik.Map(TILE_WIDTH, TILE_HEIGHT, "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over")
        map.background = mapnik.Color("#f2f3f7")
        #defining the feature layer
        geometryField = utils.calcGeometryField(shapefile.geom_type)
        field = "'NOM'"
        field2 = "'CODE'"
        query = '(select ' + geometryField + ', attribute_value->' + field +' as label, id_relat from "shapefile_feature" where' + ' shapefile_id in (' + str(shapefile.id) + ')) as geom'

        feature = Feature.objects.filter(shapefile__id=shapefile_id).geojson()
        adapter = DjLayer(feature)
        lyr = adapter.to_mapnik()
        lyr.srs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over"
        m = mapnik.Map(TILE_WIDTH, TILE_HEIGHT, "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over")




##        datasource = mapnik.PostGIS(user=dbSettings['USER'],
##                        password=dbSettings['PASSWORD'],
##                        dbname=dbSettings['NAME'],
##                        port=5433,
##                        table=query,
##                        srid=3857,
##                        geometry_field=geometryField,
##                        simplify_geometries=True,
##                        geometry_table='"shapefile_feature"')

##        featureLayer = mapnik.Layer("featureLayer")
##        featureLayer.srs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over"
##        featureLayer.datasource = datasource
##        featureLayer.styles.append("featureLayerStyle")
##
        #defining the feature layer styles
        rule = mapnik.Rule()
        if shapefile.geom_type in ["Point", "MultiPoint"]:
            rule.symbols.append(mapnik.PointSymbolizer())
        elif shapefile.geom_type in ["LineString", "MultiLineString"]:
            rule.symbols.append(mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))
        elif shapefile.geom_type in ["Polygon", "MultiPolygon"]:
            rule.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color("#f7edee")))
            rule.symbols.append(mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))
##
##        label = mapnik.TextSymbolizer(mapnik.Expression('[label]'), 'DejaVu Sans Book', 10, mapnik.Color('black'))
##        label.halo_fill = mapnik.Color('white')
##        label.halo_radius = 4
##        label.label_placement = mapnik.label_placement.INTERIOR_PLACEMENT
##        label.allow_overlap = True
##        label.avoid_edges = True
##        rule.symbols.append(label)
        style = mapnik.Style()
        style.rules.append(rule)
        lyr.styles.append('name')

##
##        #add new feature to the map
        m.append_style("name", style)
        m.layers.append(lyr)
##        map.layers.append(featureLayer)

        #rendering the map tile
        mapnik.save_map(m, "../tilestache/%s/%s.xml" % (str(request.user), str(shapefile.filename)))

        config = {
          "cache": {
            "name": "Disk",
            "path": "../tilestache/%s" % (request.user),
            "umask": "0000",
            "dirs": "portable"},
          "layers": {
            shapefile.filename: {
                "provider": {"name": "mapnik", "mapfile": "../tilestache/%s/%s.xml" % (request.user, shapefile.filename)},
                "projection": "spherical mercator"
            }
          }
        }

        # like http://tile.openstreetmap.org/1/0/0.png
        #coord = ModestMaps.Core.Coordinate(y, x, zoom)
        path = "/%s/%s/%s/%s.png" % (shapefile.filename,zoom,x,y)
        config = TileStache.Config.buildConfiguration(config)
        #type, bytes = TileStache.getTile(config.layers[shapefile.filename], coord, 'png')
        type, bytes = TileStache.requestHandler(config, path)
        return HttpResponse(bytes, mimetype="image/png")

    except:
        traceback.print_exc()
        return HttpResponse("")
Example #17
0
def xml_map_config(request, zoom, x, y, basqui_map, layersMapOptions):
    basqui_map.changed=False
    basqui_map.save()
    map = mapnik.Map(TILE_WIDTH, TILE_HEIGHT, "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over")

    for layer in layersMapOptions.all():
        try:
            layerLabel = LayerLabel.objects.get(layerMapOptions=layer)
        except ObjectDoesNotExist:
            layerLabel = None
        for layerStyle in layer.styles.all():
            shapefile = layer.layer
            geometryField = utils.calcGeometryField(shapefile.geom_type)
            if layerLabel:
                query = '(select ' + geometryField + ', attribute_value->\''+ layerLabel.field.name +'\' as label, id_relat from "shapefile_feature" where' + ' shapefile_id in (' + str(shapefile.id) + ')) as geom'
            else:
                query = '(select ' + geometryField + ', id_relat from "shapefile_feature" where' + ' shapefile_id in (' + str(shapefile.id) + ')) as geom'

            datasource = mapnik.PostGIS(user=dbSettings['USER'],
                            password=dbSettings['PASSWORD'],
                            dbname=dbSettings['NAME'],
                            port=5433,
                            table=query,
                            srid=3857,
                            geometry_field=geometryField,
                            geometry_table='"shapefile_feature"')

            featureLayer = mapnik.Layer(str(shapefile.filename) + str(layerStyle.id))
            featureLayer.srs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over"
            featureLayer.datasource = datasource
            featureLayer.styles.append(str(shapefile.filename) + '_Style' + str(layerStyle.id))

            #defining the feature layer styles
            style_rule = mapnik.Rule()
            if shapefile.geom_type in ["Point", "MultiPoint"]:
                s = mapnik.PointSymbolizer(mapnik.PathExpression(str(layerStyle.iconName)))
                s.allow_overlap = True
                style_rule.symbols.append(s)
            elif shapefile.geom_type in ["LineString", "MultiLineString"]:
                style_rule.symbols.append(mapnik.LineSymbolizer(mapnik.Color(str(layerStyle.strokeColor)), layerStyle.strokeWeight))
            elif shapefile.geom_type in ["Polygon", "MultiPolygon"]:
                p = mapnik.PolygonSymbolizer(mapnik.Color(str((layerStyle.fillColor))))
                p.fill_opacity = layerStyle.fillOpacity
                style_rule.symbols.append(p)
                style_rule.symbols.append(mapnik.LineSymbolizer(mapnik.Color(str(layerStyle.strokeColor)), layerStyle.strokeWeight))
            layer_style = mapnik.Style()
            layer_style.rules.append(style_rule)

            #defining label styles
            if layerLabel:
                label_rule = mapnik.Rule()
                label = mapnik.TextSymbolizer(mapnik.Expression('[label]'), 'DejaVu Sans Book', layerLabel.font_size, mapnik.Color(str(layerLabel.font_color)))
                label.halo_fill = mapnik.Color(str(layerLabel.halo_color))
                label.halo_radius = int(layerLabel.halo_radius)
                #label.label_placement = mapnik.label_placement.INTERIOR_PLACEMENT
                label.allow_overlap = False
                label.avoid_edges = True
                label_rule.symbols.append(label)
                label_style = mapnik.Style()
                label_style.rules.append(label_rule)
                featureLayer.styles.append(str(shapefile.filename) + '_label' + str(layerStyle.id))
                #add label to the map
                map.append_style(str(shapefile.filename) + '_label' + str(layerStyle.id), label_style)

            #add new feature to the map
            map.append_style(str(shapefile.filename) + '_Style' + str(layerStyle.id), layer_style)
            map.layers.append(featureLayer)

    #saving the map mapnik xml
    mapnik.save_map(map, "c:\\basqui\\tilestache\\%s\\%s.xml" % (str(request.user), str(basqui_map.map_name)))
Example #18
0
def importData(file, characterEncoding, format, user, folder):
    cursor = connection.cursor()
    start_time = time.time()
    #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.lower().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
    try:
        srcPath = os.path.join(dirname,shapefileName)
        srcLayers = fiona.listlayers(srcPath)
        shapefileOK = True
    except:
        traceback.print_exc()
        shapefileOK = False
    if not shapefileOK:
        os.remove(fname)
        shutil.rmtree(dirname)
        return "Not a valid vector file.", None

    #add shapefile object to database
    try:
        for i in srcLayers:
            with fiona.open(srcPath) as c:
                srcSpatialRef = to_string(c.crs)
                print srcSpatialRef
                project = CoordTransform(SpatialReference(srcSpatialRef),SpatialReference(3857))
                geometryType = c.schema['geometry']
                shapefile = Shapefile.objects.create(filename=c.name, parent=folder, srs_wkt=srcSpatialRef, geom_type=geometryType, encoding=characterEncoding, created_by=user)

                #define shapefile's attributes
                for keys, values in c.schema['properties'].iteritems():
                    dict = {}
                    dict['name'] = keys
                    props = re.split('\W+', values)
                    dict['type'] = utils.fionaTypeToInt(props[0])
                    try:
                        dict['width'] = int(props[1])
                    except IndexError:
                        dict['width'] = 0
                    if dict['type'] == 2:
                        try:
                            dict['precision'] = int(props[2])
                        except IndexError:
                            dict['precision'] = 15
                    else:
                        dict['precision'] = 0
                    attr = Attribute.objects.create(shapefile=shapefile, **dict)

                #store shapefile's features
                for srcFeature in c:
                    try:
                        wkt = dumps(srcFeature['geometry'])
                        geosGeometry = GEOSGeometry(wkt)
                        geosGeometry.srid = SpatialReference(srcSpatialRef).srid
                        geosGeometry.transform(project)
                    except TypeError:
                        geosGeometry = None

                    geometryField = utils.calcGeometryField(geometryType)

                    args = {}
                    args['shapefile'] = shapefile
                    args[geometryField] = geosGeometry
                    args['attribute_value'] = srcFeature['properties']
                    args['id_relat'] = srcFeature['id']
                    feature = Feature.objects.create(**args)

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

    except BaseException, e:
        #cleaning up
        os.remove(fname)
        shutil.rmtree(dirname, ignore_errors=False, onerror=handleRemoveReadonly)
        shapefile.delete()
        return e, None
Example #19
0
def tile(request, version, shapefile_id, zoom, x, y):
    shapefile = None
    try:
        if version != "1.0":
            raise Http404
        try:
            shapefile = Shapefile.objects.get(id=shapefile_id)
        except Shapefile.DoesNotExist:
            raise Http404
        zoom = int(zoom)
        x = int(x)
        y = int(y)

        if zoom < 0 or zoom > MAX_ZOOM_LEVEL:
            raise Http404

        xExtent = _unitsPerPixel(zoom) * TILE_WIDTH
        yExtent = _unitsPerPixel(zoom) * TILE_HEIGHT

        minLong = xExtent * x - 180.0
        minLat = yExtent * y - 90.0
        maxLong = minLong + xExtent
        maxLat = minLat + xExtent

        #if (minLong < -180 or maxLong > 180 or
        #minLat < -90 or maxLat > 90):
        #raise Http404

        map = mapnik.Map(TILE_WIDTH, TILE_HEIGHT, "+proj=longlat +datum=WGS84")
        map.background = mapnik.Color("#7391ad")

        dbSettings = settings.DATABASES['default']
        datasource = mapnik.PostGIS(host='localhost',
                                    user='******',
                                    password='******',
                                    dbname='spatial',
                                    table='"shapeEditor_basemap"',
                                    srid=4326,
                                    geometry_field="geometry",
                                    geometry_table='"shapeEditor_basemap"')

        baseLayer = mapnik.Layer("baseLayer")
        baseLayer.datasource = datasource
        baseLayer.styles.append("baseLayerStyle")

        rule = mapnik.Rule()
        rule.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color("#b5d19c")))
        rule.symbols.append(mapnik.LineSymbolizer(mapnik.Color("#000000"),
                                                  0.1))
        style = mapnik.Style()
        style.rules.append(rule)

        map.append_style("baseLayerStyle", style)
        map.layers.append(baseLayer)

        geometryField = utils.calcGeometryField(shapefile.geom_type)
        query = '(select ' + geometryField + ' from "shapeEditor_feature" where' + ' shapefile_id=' + str(
            shapefile.id) + ') as geom'
        datasource = mapnik.PostGIS(host='localhost',
                                    user='******',
                                    password='******',
                                    dbname='spatial',
                                    table=query,
                                    srid=4326,
                                    geometry_field=geometryField,
                                    geometry_table='"shapeEditor_feature"')

        featureLayer = mapnik.Layer("featureLayer")
        featureLayer.datasource = datasource
        featureLayer.styles.append("featureLayerStyle")

        rule = mapnik.Rule()
        if shapefile.geom_type in ["Point", "MultiPoint"]:
            rule.symbols.append(mapnik.PointSymbolizer())
        elif shapefile.geom_type in ["LineString", "MultiLineString"]:
            rule.symbols.append(
                mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))
        elif shapefile.geom_type in ["Polygon", "MultiPolygon"]:
            rule.symbols.append(
                mapnik.PolygonSymbolizer(mapnik.Color("#f7edee")))
            rule.symbols.append(
                mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))
        style = mapnik.Style()
        style.rules.append(rule)

        map.append_style("featureLayerStyle", style)
        map.layers.append(featureLayer)
        map.zoom_to_box(mapnik.Box2d(minLong, minLat, maxLong, maxLat))
        image = mapnik.Image(TILE_WIDTH, TILE_HEIGHT)
        mapnik.render(map, image)
        imageData = image.tostring('png')
        return HttpResponse(imageData, content_type="image/png")

    except:
        traceback.print_exc()
        return HttpResponse("")
Example #20
0
def tileLayer(request, version, shapefile_id, zoom, x, y):
    try:
        if version != "1.0":
            raise Http404
        try:
            shapefile = Shapefile.objects.get(id=shapefile_id, created_by=request.user)
            geometryField = utils.calcGeometryField(shapefile.geom_type)
            layerExtent = " ".join(map(str, (shapefile.feature_set.extent(field_name=geometryField))))

        except Shapefile.DoesNotExist:
            raise Http404
        zoom = int(zoom)
        x = int(x)
        y = int(y)
        if zoom < 0 or zoom > MAX_ZOOM_LEVEL:
            raise Http404
        xExtent = _unitsPerPixel(zoom)
        yExtent = _unitsPerPixel(zoom)
        minLong = x * xExtent - 20037508.34
        minLat = y * yExtent - 20037508.34
        maxLong = minLong + xExtent
        maxLat = minLat + yExtent
        if minLong < -20037508.34 or maxLong > 20037508.34 or minLat < -20037508.34 or maxLat > 20037508.34:
            raise Http404

        # create de mapnik.map object
        mapXML = mapnik.Map(
            TILE_WIDTH,
            TILE_HEIGHT,
            "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over",
        )
        mapXML.buffer_size = 128

        # defining the feature layer
        query = (
            "(select "
            + geometryField
            + ', id_relat as label from "layers_feature" WHERE shapefile_id = '
            + str(shapefile.id)
            + ") as geom"
        )

        datasource = mapnik.PostGIS(
            host=dbSettings["HOST"],
            user=dbSettings["USER"],
            password=dbSettings["PASSWORD"],
            dbname=dbSettings["NAME"],
            port=dbSettings["PORT"],
            table=query,
            srid=3857,
            estimate_extent=False,
            extent=layerExtent,
            geometry_field=geometryField,
            simplify_geometries=True,
            geometry_table='"layers_feature"',
        )

        featureLayer = mapnik.Layer("featureLayer")
        featureLayer.srs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over"
        featureLayer.cache_features = True
        featureLayer.datasource = datasource
        featureLayer.styles.append("featureLayerStyle")

        # defining the feature layer styles
        rule = mapnik.Rule()
        if shapefile.geom_type in ["Point", "3D Point", "MultiPoint", "3D MultiPoint"]:
            rule.symbols.append(mapnik.PointSymbolizer())
        elif shapefile.geom_type in ["LineString", "3D LineString", "MultiLineString", "3D MultiLineString"]:
            rule.symbols.append(mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))
        elif shapefile.geom_type in ["Polygon", "3D Polygon", "MultiPolygon", "3D MultiPolygon"]:
            rule.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color("#f7edee")))
            rule.symbols.append(mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))
        style = mapnik.Style()
        style.rules.append(rule)
        mapXML.append_style("featureLayerStyle", style)

        label_rule = mapnik.Rule()
        label = mapnik.TextSymbolizer(mapnik.Expression("[label]"), "DejaVu Sans Book", 10, mapnik.Color("black"))
        label.halo_radius = 4
        label.allow_overlap = False
        label.avoid_edges = True
        label_rule.symbols.append(label)
        label_style = mapnik.Style()
        label_style.rules.append(label_rule)
        featureLayer.styles.append("featureLayerStyle_label")
        # add label to the map
        mapXML.append_style("featureLayerStyle_label", label_style)

        # add new feature to the map
        mapXML.layers.append(featureLayer)

        # rendering the map tile
        mapnik_xml_path = "../tilestache/%s/layers/vector/viewer/%s.xml" % (str(request.user), str(shapefile.name))
        mapnik.save_map(mapXML, mapnik_xml_path)

        config = {
            "cache": {"name": "Test", "path": "../tilestache/%s" % (request.user), "umask": "0000", "dirs": "portable"},
            "layers": {
                shapefile.name: {
                    "provider": {"name": "mapnik", "mapfile": mapnik_xml_path},
                    "metatile": {"rows": 2, "columns": 2, "buffer": 64},
                    "projection": "spherical mercator",
                    "write cache": False,
                }
            },
        }

        # like http://tile.openstreetmap.org/1/0/0.png
        # coord = ModestMaps.Core.Coordinate(y, x, zoom)
        path = "/%s/%s/%s/%s.png" % (shapefile.name, zoom, x, y)
        config = TileStache.Config.buildConfiguration(config)
        # type, bytes = TileStache.getTile(config.layers[shapefile.filename], coord, 'png')
        type, bytes = TileStache.requestHandler(config, path)
        return HttpResponse(bytes, content_type="image/png")

    except:
        traceback.print_exc()
        return HttpResponse("")
Example #21
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 #22
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 #23
0
def importData(file, characterEncoding, format, user, folder):
    cursor = connection.cursor()
    start_time = time.time()
    #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.lower().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
    try:
        srcPath = os.path.join(dirname, shapefileName)
        srcLayers = fiona.listlayers(srcPath)
        shapefileOK = True
    except:
        traceback.print_exc()
        shapefileOK = False
    if not shapefileOK:
        os.remove(fname)
        shutil.rmtree(dirname)
        return "Not a valid vector file.", None

    #add shapefile object to database
    try:
        for i in srcLayers:
            with fiona.open(srcPath) as c:
                srcSpatialRef = to_string(c.crs)
                print srcSpatialRef
                project = CoordTransform(SpatialReference(srcSpatialRef),
                                         SpatialReference(3857))
                geometryType = c.schema['geometry']
                shapefile = Shapefile.objects.create(
                    filename=c.name,
                    parent=folder,
                    srs_wkt=srcSpatialRef,
                    geom_type=geometryType,
                    encoding=characterEncoding,
                    created_by=user)

                #define shapefile's attributes
                for keys, values in c.schema['properties'].iteritems():
                    dict = {}
                    dict['name'] = keys
                    props = re.split('\W+', values)
                    dict['type'] = utils.fionaTypeToInt(props[0])
                    try:
                        dict['width'] = int(props[1])
                    except IndexError:
                        dict['width'] = 0
                    if dict['type'] == 2:
                        try:
                            dict['precision'] = int(props[2])
                        except IndexError:
                            dict['precision'] = 15
                    else:
                        dict['precision'] = 0
                    attr = Attribute.objects.create(shapefile=shapefile,
                                                    **dict)

                #store shapefile's features
                for srcFeature in c:
                    try:
                        wkt = dumps(srcFeature['geometry'])
                        geosGeometry = GEOSGeometry(wkt)
                        geosGeometry.srid = SpatialReference(
                            srcSpatialRef).srid
                        geosGeometry.transform(project)
                    except TypeError:
                        geosGeometry = None

                    geometryField = utils.calcGeometryField(geometryType)

                    args = {}
                    args['shapefile'] = shapefile
                    args[geometryField] = geosGeometry
                    args['attribute_value'] = srcFeature['properties']
                    args['id_relat'] = srcFeature['id']
                    feature = Feature.objects.create(**args)

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

    except BaseException, e:
        #cleaning up
        os.remove(fname)
        shutil.rmtree(dirname,
                      ignore_errors=False,
                      onerror=handleRemoveReadonly)
        shapefile.delete()
        return e, None
Example #24
0
def exportData(shapefile):

    dstDir = tempfile.mkdtemp()
    dstFile = str(os.path.join(dstDir, shapefile.filename))

    dstSpatialRef = osr.SpatialReference()
    dstSpatialRef.ImportFromWkt(shapefile.srs_wkd)

    driver = ogr.GetDriverByName("ESRI Shapefile")
    datasource = driver.CreateDataSource(dstFile)
    layer = datasource.CreateLayer(str(shapefile.filename), dstSpatialRef)

    # find the spatial reference
    srcSpatialRef = osr.SpatialReference()
    srcSpatialRef.ImportFromEPSG(4326)
    coordTransform = osr.CoordinateTransformation(srcSpatialRef, dstSpatialRef)

    geomField = utils.calcGeometryField(shapefile.geom_type)

    # Define layer attributes
    for attr in shapefile.attribute_set.all():
        field = ogr.FieldDefn(str(attr.name), attr.type)
        field.SetWidth(attr.width)
        field.SetPrecision(attr.precision)
        layer.CreateField(field)

    # Add all the (Single type) geometries for this layer
    for feature in shapefile.feature_set.all():

        # Geometry is a django construct inherited from GeometryField

        geometry = getattr(feature, geomField)
        geometry = utils.unwrapGEOSGeometry(geometry)

        dstGeometry = ogr.CreateGeometryFromWkt(geometry.wkt)
        dstGeometry.Transform(coordTransform)

        dstFeature = ogr.Feature(layer.GetLayerDefn())
        dstFeature.SetGeometry(dstGeometry)

        # add in the feature's attributes
        for attrValue in feature.attributevalue_set.all():
            utils.setOGRFeatureAttribute(attrValue.attribute, attrValue.value,
                                         dstFeature, shapefile.encoding)

        layer.CreateFeature(dstFeature)
        dstFeature.Destroy()

    datasource.Destroy()

    # Compress the shapefile
    temp = tempfile.TemporaryFile()
    zipHandle = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)

    for fName in os.listdir(dstDir):
        zipHandle.write(os.path.join(dstDir, fName), fName)
    zipHandle.close()

    # useful links to temp's directory
    #  shapefileBase = os.path.splitext ( dstFile) [0]
    shapefileName = os.path.splitext(shapefile.filename)[0]

    # Delete the temporary files
    shutil.rmtree(dstDir)

    # Return the zip archive to the user
    f = FileWrapper(temp)
    response = HttpResponse(f, content_type="application\zip")
    response ['Content-Disposition'] = \
       "attachment; filename=" + shapefileName + ".zip"
    response['Content-Length'] = temp.tell()
    temp.seek(0)
    return response
Example #25
0
def tileFeature(request, version, shapefile_id, feature_id, zoom, x, y):
    try:
        if version != "1.0":
            raise Http404
        try:
            shapefile = Shapefile.objects.get(id=shapefile_id,
                                              created_by=request.user)
        except Shapefile.DoesNotExist:
            raise Http404
        zoom = int(zoom)
        x = int(x)
        y = int(y)
        if zoom < 0 or zoom > MAX_ZOOM_LEVEL:
            raise Http404
        xExtent = _unitsPerPixel(zoom)
        yExtent = _unitsPerPixel(zoom)
        minLong = x * xExtent - 20037508.34
        minLat = y * yExtent - 20037508.34
        maxLong = minLong + xExtent
        maxLat = minLat + yExtent
        if (minLong < -20037508.34 or maxLong > 20037508.34
                or minLat < -20037508.34 or maxLat > 20037508.34):
            raise Http404

        #create mapnik.map object
        map = mapnik.Map(
            TILE_WIDTH, TILE_HEIGHT,
            "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over"
        )
        map.background = mapnik.Color("#fff")
        #defining the feature layer
        geometryField = utils.calcGeometryField(shapefile.geom_type)
        query = '(select ' + geometryField + ', id, id_relat as label from "layers_feature" WHERE shapefile_id = ' + str(
            shapefile.id) + ' AND id = ' + str(feature_id) + ') as geom'

        datasource = mapnik.PostGIS(host=dbSettings['HOST'],
                                    user=dbSettings['USER'],
                                    password=dbSettings['PASSWORD'],
                                    dbname=dbSettings['NAME'],
                                    port=dbSettings['PORT'],
                                    table=query,
                                    srid=3857,
                                    geometry_field=geometryField,
                                    simplify_geometries=True,
                                    geometry_table='"layers_feature"')

        featureLayer = mapnik.Layer("featureLayer")
        featureLayer.srs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over"
        featureLayer.datasource = datasource
        featureLayer.styles.append("featureLayerStyle")

        #defining the feature layer styles
        rule = mapnik.Rule()
        if shapefile.geom_type in [
                "Point", "3D Point", "MultiPoint", "3D MultiPoint"
        ]:
            rule.symbols.append(mapnik.PointSymbolizer())
        elif shapefile.geom_type in [
                "LineString", "3D LineString", "MultiLineString",
                "3D MultiLineString"
        ]:
            rule.symbols.append(
                mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))
        elif shapefile.geom_type in [
                "Polygon", "3D Polygon", "MultiPolygon", "3D MultiPolygon"
        ]:
            rule.symbols.append(
                mapnik.PolygonSymbolizer(mapnik.Color("#f7edee")))
            rule.symbols.append(
                mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))
        style = mapnik.Style()
        style.rules.append(rule)
        map.append_style("featureLayerStyle", style)

        label_rule = mapnik.Rule()
        label = mapnik.TextSymbolizer(mapnik.Expression('[label]'),
                                      'DejaVu Sans Book', 10,
                                      mapnik.Color('black'))
        label.halo_radius = 4
        label.allow_overlap = False
        label.avoid_edges = True
        label_rule.symbols.append(label)
        label_style = mapnik.Style()
        label_style.rules.append(label_rule)
        featureLayer.styles.append("featureLayerStyle_label")
        #add label to the map
        map.append_style("featureLayerStyle_label", label_style)

        #add new feature to the map
        map.layers.append(featureLayer)

        #rendering the map tile
        mapnik_xml_path = "../tilestache/%s/layers/vector/lightViewer/%s.xml" % (
            str(request.user), str(shapefile.name))
        mapnik.save_map(map, mapnik_xml_path)

        config = {
            "cache": {
                "name": "Test",
                "path": "../tilestache/%s" % (request.user),
                "umask": "0000",
                "dirs": "portable"
            },
            "layers": {
                shapefile.name: {
                    "provider": {
                        "name": "mapnik",
                        "mapfile": mapnik_xml_path
                    },
                    "metatile": {
                        "rows": 2,
                        "columns": 2,
                        "buffer": 64
                    },
                    "projection": "spherical mercator",
                    "write cache": False
                }
            }
        }

        path = "/%s/%s/%s/%s.png" % (shapefile.name, zoom, x, y)
        config = TileStache.Config.buildConfiguration(config)
        type, bytes = TileStache.requestHandler(config, path)
        return HttpResponse(bytes, content_type="image/png")

    except:
        traceback.print_exc()
        return HttpResponse("")
Example #26
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.
Example #27
0
def exportMultiLayersData(layers, encoding, EPSG, format):
    try:
        dstSpatialRef = osr.SpatialReference()
        dstSpatialRef.ImportFromEPSG(EPSG)
        driver = ogr.GetDriverByName(ogrDriver_dic[format])
        dstDir = tempfile.mkdtemp()
        dstFile = str(os.path.join(dstDir, layers[0].name+filenameExt_dic[format]))
        datasource = driver.CreateDataSource(dstFile)

        for layer in layers:
            vlayer = datasource.CreateLayer(str(layer.name),dstSpatialRef)

            #retrive attributes
            for attr in layer.attribute_set.all():
                field = ogr.FieldDefn(str(attr.name), attr.type)
                field.SetWidth(attr.width)
                field.SetPrecision(attr.precision)
                vlayer.CreateField(field)

            #save features in shapefile
            srcSpatialRef = osr.SpatialReference()
            srcSpatialRef.ImportFromEPSG(3857)
            coordTransform = osr.CoordinateTransformation(srcSpatialRef, dstSpatialRef)
            geomField = utils.calcGeometryField(layer.geom_type)
            for feature in layer.feature_set.all().order_by('id_relat'):

                if geometry:
                    geometry = getattr(feature, geomField)
                    geometry = utils.unwrapGEOSGeometry(geometry)
                    dstGeometry = ogr.CreateGeometryFromWkt(geometry.wkt)
                    dstGeometry.Transform(coordTransform)
                else:
                    dstGeometry = None

            #save attributes in the shapefile
                dstFeature = ogr.Feature(vlayer.GetLayerDefn())
                dstFeature.SetGeometry(dstGeometry)
                for attrName, attrValue in feature.attribute_value.iteritems():
                    attribute = Attribute.objects.get(name=str(attrName), shapefile=feature.shapefile)
                    utils.setOGRFeatureAttribute(attribute, attrValue, dstFeature, encoding)
                vlayer.CreateFeature(dstFeature)
                dstFeature.Destroy()
        datasource.Destroy()

        #compress the shapefile
        temp = tempfile.TemporaryFile()
        zip = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
        shapefileBase = os.path.splitext(dstFile)[0]
        shapefileName = os.path.splitext(layer.name)[0]
        for fName in os.listdir(dstDir):
            zip.write(os.path.join(dstDir, fName), fName)
        zip.close()

        #delete temporary files
        shutil.rmtree(dstDir)

        #return the zip to user
        f = FileWrapper(temp)
        response = StreamingHttpResponse(f, content_type="application/zip")
        response['Content-Disposition'] = "attachment; filename=" + shapefileName + fileExt_dic[format]
        response['Content-Length'] = temp.tell()
        temp.seek(0)

        return None, response

    except BaseException, e:
        return e.rstrip('\n'), None
Example #28
0
def tile(request, version, shapefile_id, zoom, x, y):

    shapefile = None
    try:

        if version != "1.0":
            raise Http404
        try:
            shapefile = Shapefile.objects.get(id=shapefile_id)
        except Shapefile.DoesNotExist:
            raise Http404

        zoom = int(zoom)
        x = int(x)
        y = int(y)

        # min(x) = 0, max (x) =

        if zoom < 0 or zoom > MAX_ZOOM_LEVEL:
            raise Http404

        # for TILE_WIDTH/HEIGHT==256
        # at zoom = 0 extents will be 180 units/degrees

        xExtent = _unitsPerPixel(zoom) * TILE_WIDTH
        yExtent = _unitsPerPixel(zoom) * TILE_HEIGHT

        minLong = xExtent * x - 180.0
        minLat = yExtent * y - 90.0

        maxLong = minLong + xExtent
        maxLat = minLat + xExtent

        if (minLong < -180 or maxLong > 180 or minLat < -90 or maxLat > 90):
            print "bound error raised"
            raise Http404

        dbFile = "/home/john360/computing/geospatial_dev/geodjango_shapefile_app/DB/geodjango.db"
        extentStr = "%s,%s,%s,%s" % (minLong, minLat, maxLong, maxLat)

        map = mapnik.Map(TILE_WIDTH, TILE_HEIGHT,
                         '+proj=longlat +datum=WGS84 +no_defs')

        map.background = mapnik.Color("#7391ad")

        #    time.sleep (0.3)

        # Set up the base layer
        datasource = \
                 mapnik.SQLite(file=dbFile,
                 table="shapeEditor_baseMap",
                 key_field="id",
        #             srid=4326,
                 geometry_field="geometry",
                 extent=extentStr,
                 wkb_format="spatialite")

        baseLayer = mapnik.Layer("baseLayer")
        baseLayer.datasource = datasource
        baseLayer.styles.append("baseLayerStyle")

        rule = mapnik.Rule()

        rule.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color("#b5d19c")))

        rule.symbols.append(mapnik.LineSymbolizer(mapnik.Color("#404040"),
                                                  0.2))

        style = mapnik.Style()
        style.rules.append(rule)

        map.append_style("baseLayerStyle", style)
        map.layers.append(baseLayer)

        # Define the feature layer

        geometryField = utils.calcGeometryField(shapefile.geom_type)

        query = '( select ' + geometryField  +  \
                ' from "shapeEditor_feature" where ' + \
                'shapefile_id = ' + str(shapefile.id) + ' ) as geom'

        dbSettings = settings.DATABASES['default']

        datasource = \
                 mapnik.PostGIS(user=dbSettings['USER'],
                                password=dbSettings['PASSWORD'],
                                dbname=dbSettings['NAME'],
                                table=query,
                                srid=4326,
                                geometry_field=geometryField,
                                geometry_table='"shapeEditor_feature"')

        featureLayer = mapnik.Layer("featureLayer")
        featureLayer.datasource = datasource
        featureLayer.styles.append("featureLayerStyle")

        rule = mapnik.Rule()

        if shapefile.geom_type in ["Point", "MultiPoint"]:
            rule.symbols.append(
                mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))

        elif shapefile.geom_type in ["Polygon", "Multipolygon"]:

            rule.symbols.append(
                mapnik.PolygonSymbolizer(mapnik.Color("#b5d19c")))
            rule.symbols.append(
                mapnik.LineSymbolizer(mapnik.Color("#000000"), 1))

        style = mapnik.Style()
        style.rules.append(rule)

        map.append_style("featureLayerStyle", style)
        map.layers.append(featureLayer)

        map.zoom_to_box(mapnik.Envelope(minLong, minLat, maxLong, maxLat))
        image = mapnik.Image(TILE_WIDTH, TILE_HEIGHT)

        mapnik.render(map, image)
        imageData = image.tostring('png')

        return HttpResponse(imageData, mimetype="image/png")

    except:
        traceback.print_exc()
        return HttpResponse(" ")
Example #29
0
def tile(request, version, shapefile_id, zoom, x, y):
    try:
        if version != "1.0":
            raise Http404
        try:
            shapefile = Shapefile.objects.get(id=shapefile_id)
        except Shapefile.DoesNotExist:
            raise Http404
        zoom = int(zoom)
        x = int(x)
        y = int(y)
        if zoom < 0 or zoom > MAX_ZOOM_LEVEL:
            raise Http404
        xExtent = _unitsPerPixel(zoom) * TILE_WIDTH
        yExtent = _unitsPerPixel(zoom) * TILE_HEIGHT
        minLong = x * xExtent - 180.0
        minLat = y * yExtent - 90.0
        maxLong = minLong + xExtent
        maxLat = minLat + yExtent
        if (minLong < -180 or maxLong > 180 or
            minLat < -90 or maxLat > 90):
            raise Http404
        map = mapnik.Map(TILE_WIDTH, TILE_HEIGHT,
                        "+proj=longlat +datum=WGS84")
        map.background = mapnik.Color("#7391ad")
        dbSettings = settings.DATABASES['default']
        datasource = \
            mapnik.PostGIS(user=dbSettings['USER'],
                            password=dbSettings['PASSWORD'],
                            dbname=dbSettings['NAME'],
                            table='"shapeEditor_basemap"',
                            srid=4326,
                            geometry_field="geometry",
                            geometry_table='"shapeEditor_basemap"')
        baseLayer = mapnik.Layer("baseLayer")
        baseLayer.datasource = datasource
        baseLayer.styles.append("baseLayerStyle")
        rule = mapnik.Rule()
        rule.symbols.append(
            mapnik.PolygonSymbolizer(mapnik.Color("#b5d19c")))
        rule.symbols.append(
            mapnik.LineSymbolizer(mapnik.Color("#404040"), 0.2))
        style = mapnik.Style()
        style.rules.append(rule)
        map.append_style("baseLayerStyle", style)
        map.layers.append(baseLayer)
        geometryField = utils.calcGeometryField(shapefile.geom_type)
        query = '(select ' + geometryField \
            + ' from "shapeEditor_feature" where' \
            + ' shapefile_id=' + str(shapefile.id) + ') as geom'
        datasource = \
            mapnik.PostGIS(user=dbSettings['USER'],
                            password=dbSettings['PASSWORD'],
                            dbname=dbSettings['NAME'],
                            table=query,
                            srid=4326,
                            geometry_field=geometryField,
                            geometry_table='"shapeEditor_feature"')
        featureLayer = mapnik.Layer("featureLayer")
        featureLayer.datasource = datasource
        featureLayer.styles.append("featureLayerStyle")
        rule = mapnik.Rule()
        if shapefile.geom_type in ["Point", "MultiPoint"]:
            rule.symbols.append(mapnik.PointSymbolizer())
        elif shapefile.geom_type in ["LineString", "MultiLineString"]:
            rule.symbols.append(
                mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))
        elif shapefile.geom_type in ["Polygon", "MultiPolygon"]:
            rule.symbols.append(
                mapnik.PolygonSymbolizer(mapnik.Color("#f7edee")))
            rule.symbols.append(
                mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))
        style = mapnik.Style()
        style.rules.append(rule)
        map.append_style("featureLayerStyle", style)
        map.layers.append(featureLayer)
        map.zoom_to_box(mapnik.Envelope(minLong, minLat,
                        maxLong, maxLat))
        image = mapnik.Image(TILE_WIDTH, TILE_HEIGHT)
        mapnik.render(map, image)
        imageData = image.tostring('png')
        return HttpResponse(imageData, mimetype="image/png")
    except:
        traceboack.print_exc()
        return HttpResponse("")
Example #30
0
def tileLayer(request, version, shapefile_id, zoom, x, y):
    try:
        if version != "1.0":
            raise Http404
        try:
            shapefile = Shapefile.objects.get(id=shapefile_id,
                                              created_by=request.user)
        except Shapefile.DoesNotExist:
            raise Http404
        zoom = int(zoom)
        x = int(x)
        y = int(y)
        if zoom < 0 or zoom > MAX_ZOOM_LEVEL:
            raise Http404
        xExtent = _unitsPerPixel(zoom)
        yExtent = _unitsPerPixel(zoom)
        minLong = x * xExtent - 20037508.34
        minLat = y * yExtent - 20037508.34
        maxLong = minLong + xExtent
        maxLat = minLat + yExtent
        if (minLong < -20037508.34 or maxLong > 20037508.34
                or minLat < -20037508.34 or maxLat > 20037508.34):
            raise Http404

        #create de mapnik.map object
        map = mapnik.Map(
            TILE_WIDTH, TILE_HEIGHT,
            "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over"
        )
        map.background = mapnik.Color("#f2f3f7")
        #defining the feature layer
        geometryField = utils.calcGeometryField(shapefile.geom_type)
        field = "'NOM'"
        field2 = "'CODE'"
        query = '(select ' + geometryField + ', attribute_value->' + field + ' as label, id_relat from "shapefile_feature" where' + ' shapefile_id in (' + str(
            shapefile.id) + ')) as geom'

        ##        datasource = mapnik.SQLite(file='C:\mygeosite\sqlite3\sql3.db',
        ##                                        table=query,
        ##                                        srid=3857,
        ##                                        geometry_field=geometryField)

        ##        datasource = mapnik.PostGIS(user=dbSettings['USER'],
        ##                        password=dbSettings['PASSWORD'],
        ##                        dbname=dbSettings['NAME'],
        ##                        table=query,
        ##                        srid=3857,
        ##                        geometry_field=geometryField,
        ##                        simplify_geometries=True,
        ##                        geometry_table='"shapefile_feature"')

        feature = Feature.objects.filter(shapefile__id=shapefile_id).geojson()
        geoj = feature.geojson
        datasource = mapnik.Ogr(layer_by_index=0, string=geoj)
        ##
        featureLayer = mapnik.Layer("featureLayer")
        featureLayer.srs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over"
        featureLayer.datasource = datasource
        featureLayer.styles.append("featureLayerStyle")

        #defining the feature layer styles
        rule = mapnik.Rule()
        if shapefile.geom_type in ["Point", "MultiPoint"]:
            rule.symbols.append(mapnik.PointSymbolizer())
        elif shapefile.geom_type in ["LineString", "MultiLineString"]:
            rule.symbols.append(
                mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))
        elif shapefile.geom_type in ["Polygon", "MultiPolygon"]:
            rule.symbols.append(
                mapnik.PolygonSymbolizer(mapnik.Color("#f7edee")))
            rule.symbols.append(
                mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))


##        label = mapnik.TextSymbolizer(mapnik.Expression('[label]'), 'DejaVu Sans Book', 10, mapnik.Color('black'))
##        label.halo_fill = mapnik.Color('white')
##        label.halo_radius = 4
##        label.label_placement = mapnik.label_placement.INTERIOR_PLACEMENT
##        label.allow_overlap = True
##        label.avoid_edges = True
##        rule.symbols.append(label)
        style = mapnik.Style()
        style.rules.append(rule)

        #add new feature to the map
        map.append_style("featureLayerStyle", style)
        map.layers.append(featureLayer)

        #rendering the map tile
        mapnik.save_map(
            map, "../tilestache/%s/%s.xml" %
            (str(request.user), str(shapefile.filename)))

        config = {
            "cache": {
                "name": "Test",
                "path": "../tilestache/%s" % (request.user),
                "umask": "0000",
                "dirs": "portable"
            },
            "layers": {
                shapefile.filename: {
                    "provider": {
                        "name":
                        "mapnik",
                        "mapfile":
                        "../tilestache/%s/%s.xml" %
                        (request.user, shapefile.filename)
                    },
                    "projection": "spherical mercator"
                }
            }
        }

        # like http://tile.openstreetmap.org/1/0/0.png
        #coord = ModestMaps.Core.Coordinate(y, x, zoom)
        path = "/%s/%s/%s/%s.png" % (shapefile.filename, zoom, x, y)
        config = TileStache.Config.buildConfiguration(config)
        #type, bytes = TileStache.getTile(config.layers[shapefile.filename], coord, 'png')
        type, bytes = TileStache.requestHandler(config, path)
        return HttpResponse(bytes, mimetype="image/png")

    except:
        traceback.print_exc()
        return HttpResponse("")
Example #31
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 #32
0
def exportData(shapefile):
    """ Export the contents of the given shapefile.

        'shapefile' is the Shapefile object to export.

        We create a shapefile which holds the contents of the given shapefile,
        then copy the shapefile into a temporary zip archive.  Upon completion,
        we return a Django HttpResponse object which can be used to send the
        zipped shapefile to the user's web browser.
    """
    # Create an OGR shapefile to hold the data we're exporting.

    dstDir = tempfile.mkdtemp()
    dstFile = str(os.path.join(dstDir, shapefile.filename))

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

    dstSpatialRef = osr.SpatialReference()
    dstSpatialRef.ImportFromWkt(shapefile.srs_wkt)

    coordTransform = osr.CoordinateTransformation(srcSpatialRef,
                                                  dstSpatialRef)

    driver = ogr.GetDriverByName("ESRI Shapefile")
    datasource = driver.CreateDataSource(dstFile)
    layer = datasource.CreateLayer(str(shapefile.filename),
                                   dstSpatialRef)

    # Define the various fields which will hold our attributes.

    for attr in shapefile.attribute_set.all():
        field = ogr.FieldDefn(str(attr.name), attr.type)
        field.SetWidth(attr.width)
        field.SetPrecision(attr.precision)
        layer.CreateField(field)

    # Save the feature geometries and attributes into the shapefile.

    geomField = utils.calcGeometryField(shapefile.geom_type)

    for feature in shapefile.feature_set.all():
        geometry = getattr(feature, geomField)
        geometry = utils.unwrapGEOSGeometry(geometry)
        dstGeometry = ogr.CreateGeometryFromWkt(geometry.wkt)
        dstGeometry.Transform(coordTransform)

        dstFeature = ogr.Feature(layer.GetLayerDefn())
        dstFeature.SetGeometry(dstGeometry)

        for attrValue in feature.attributevalue_set.all():
            utils.setOGRFeatureAttribute(attrValue.attribute,
                                         attrValue.value,
                                         dstFeature,
                                         shapefile.encoding)

        layer.CreateFeature(dstFeature)
        dstFeature.Destroy()

    datasource.Destroy() # Close the file, write everything to disk.

    # Compress the shapefile into a ZIP archive.

    temp = tempfile.TemporaryFile()
    zip = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)

    shapefileBase = os.path.splitext(dstFile)[0]
    shapefileName = os.path.splitext(shapefile.filename)[0]

    for fName in os.listdir(dstDir):
        zip.write(os.path.join(dstDir, fName), fName)

    zip.close()

    # Clean up our temporary files.

    shutil.rmtree(dstDir)

    # Create an HttpResponse object to send the ZIP file back to the user's web
    # browser.

    f = FileWrapper(temp)
    response = HttpResponse(f, content_type="application/zip")
    response['Content-Disposition'] = "attachment; filename=" \
                                    + shapefileName + ".zip"
    response['Content-Length'] = temp.tell()
    temp.seek(0)
    return response
Example #33
0
def exportMultiLayersData(layers, encoding, EPSG, format):
    try:
        dstSpatialRef = osr.SpatialReference()
        dstSpatialRef.ImportFromEPSG(EPSG)
        driver = ogr.GetDriverByName(ogrDriver_dic[format])
        dstDir = tempfile.mkdtemp()
        dstFile = str(
            os.path.join(dstDir, layers[0].name + filenameExt_dic[format]))
        datasource = driver.CreateDataSource(dstFile)

        for layer in layers:
            vlayer = datasource.CreateLayer(str(layer.name), dstSpatialRef)

            #retrive attributes
            for attr in layer.attribute_set.all():
                field = ogr.FieldDefn(str(attr.name), attr.type)
                field.SetWidth(attr.width)
                field.SetPrecision(attr.precision)
                vlayer.CreateField(field)

            #save features in shapefile
            srcSpatialRef = osr.SpatialReference()
            srcSpatialRef.ImportFromEPSG(3857)
            coordTransform = osr.CoordinateTransformation(
                srcSpatialRef, dstSpatialRef)
            geomField = utils.calcGeometryField(layer.geom_type)
            for feature in layer.feature_set.all().order_by('id_relat'):

                if geometry:
                    geometry = getattr(feature, geomField)
                    geometry = utils.unwrapGEOSGeometry(geometry)
                    dstGeometry = ogr.CreateGeometryFromWkt(geometry.wkt)
                    dstGeometry.Transform(coordTransform)
                else:
                    dstGeometry = None

            #save attributes in the shapefile
                dstFeature = ogr.Feature(vlayer.GetLayerDefn())
                dstFeature.SetGeometry(dstGeometry)
                for attrName, attrValue in feature.attribute_value.iteritems():
                    attribute = Attribute.objects.get(
                        name=str(attrName), shapefile=feature.shapefile)
                    utils.setOGRFeatureAttribute(attribute, attrValue,
                                                 dstFeature, encoding)
                vlayer.CreateFeature(dstFeature)
                dstFeature.Destroy()
        datasource.Destroy()

        #compress the shapefile
        temp = tempfile.TemporaryFile()
        zip = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
        shapefileBase = os.path.splitext(dstFile)[0]
        shapefileName = os.path.splitext(layer.name)[0]
        for fName in os.listdir(dstDir):
            zip.write(os.path.join(dstDir, fName), fName)
        zip.close()

        #delete temporary files
        shutil.rmtree(dstDir)

        #return the zip to user
        f = FileWrapper(temp)
        response = StreamingHttpResponse(f, content_type="application/zip")
        response[
            'Content-Disposition'] = "attachment; filename=" + shapefileName + fileExt_dic[
                format]
        response['Content-Length'] = temp.tell()
        temp.seek(0)

        return None, response

    except BaseException, e:
        return e.rstrip('\n'), None
Example #34
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 #35
0
def tileMap(request, version, ezmap_id, zoom, x, y):
    try:
        if version != "1.0":
            raise Http404
        try:
            ezmap = EzMap.objects.get(id=ezmap_id)
            layersMapOptions = LayerMapOptions.objects.filter(
                ezmap=ezmap, visible=True).order_by('-position')
        except Shapefile.DoesNotExist or Feature.DoesNotExist:
            raise Http404
        zoom = int(zoom)
        x = int(x)
        y = int(y)
        if zoom < 0 or zoom > MAX_ZOOM_LEVEL:
            raise Http404
        xExtent = _unitsPerPixel(zoom)
        yExtent = _unitsPerPixel(zoom)
        minLong = x * xExtent - 20037508.34
        minLat = y * yExtent - 20037508.34
        maxLong = minLong + xExtent
        maxLat = minLat + yExtent
        if (minLong < -20037508.34 or maxLong > 20037508.34
                or minLat < -20037508.34 or maxLat > 20037508.34):
            raise Http404

        if ezmap.changed:
            #create de mapnik.map object
            ezmap.changed = False
            ezmap.save()
            map = mapnik.Map(
                TILE_WIDTH, TILE_HEIGHT,
                "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over"
            )
            #map.background = mapnik.Color("#ffffff")
            #defining the feature layer
            for layer in layersMapOptions.all():
                label = LayerLabel.objects.get(layerMapOptions=layer)
                for layerStyle in layer.styles.all():
                    shapefile = layer.layer
                    geometryField = utils.calcGeometryField(
                        shapefile.geom_type)
                    query = '(select ' + geometryField + ', attribute_value->\'' + label.field.name + '\' as label, id_relat from "shapefile_feature" where' + ' shapefile_id in (' + str(
                        shapefile.id) + ')) as geom'

                    ##                    datasource = mapnik.SQLite(file='C:\mygeosite\sqlite3\sql3.db',
                    ##                                                    table=query,
                    ##                                                    srid=3857,
                    ##                                                    geometry_field=geometryField,
                    ##                                                    use_spatial_index=False)

                    datasource = mapnik.PostGIS(
                        user=dbSettings['USER'],
                        password=dbSettings['PASSWORD'],
                        dbname=dbSettings['NAME'],
                        table=query,
                        srid=3857,
                        geometry_field=geometryField,
                        geometry_table='"shapefile_feature"')

                    featureLayer = mapnik.Layer(
                        str(shapefile.filename) + str(layerStyle.id))
                    featureLayer.srs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over"
                    featureLayer.datasource = datasource
                    featureLayer.styles.append(
                        str(shapefile.filename) + '_Style' +
                        str(layerStyle.id))

                    #defining the feature layer styles
                    rule = mapnik.Rule()
                    if shapefile.geom_type in ["Point", "MultiPoint"]:
                        s = mapnik.PointSymbolizer(
                            mapnik.PathExpression(str(layerStyle.iconName)))
                        s.allow_overlap = True
                        rule.symbols.append(s)
                    elif shapefile.geom_type in [
                            "LineString", "MultiLineString"
                    ]:
                        rule.symbols.append(
                            mapnik.LineSymbolizer(
                                mapnik.Color(str(layerStyle.strokeColor)),
                                layerStyle.strokeWeight))
                    elif shapefile.geom_type in ["Polygon", "MultiPolygon"]:
                        p = mapnik.PolygonSymbolizer(
                            mapnik.Color(str((layerStyle.fillColor))))
                        p.fill_opacity = layerStyle.fillOpacity
                        rule.symbols.append(p)
                        rule.symbols.append(
                            mapnik.LineSymbolizer(
                                mapnik.Color(str(layerStyle.strokeColor)),
                                layerStyle.strokeWeight))

                    label = mapnik.TextSymbolizer(
                        mapnik.Expression('[label]'), 'DejaVu Sans Book',
                        label.font_size, mapnik.Color(str(label.font_color)))
                    label.halo_fill = mapnik.Color(str(label.halo_color))
                    label.halo_radius = int(label.halo_radius)
                    label.label_placement = mapnik.label_placement.INTERIOR_PLACEMENT
                    label.allow_overlap = False
                    label.avoid_edges = True
                    rule.symbols.append(label)
                    style = mapnik.Style()
                    style.rules.append(rule)

                    #add new feature to the map
                    map.append_style(
                        str(shapefile.filename) + '_Style' +
                        str(layerStyle.id), style)
                    map.layers.append(featureLayer)

            #saving the map mapnik xml
            mapnik.save_map(
                map, "c:\\mygeosite\\tilestache\\%s\\%s.xml" %
                (str(request.user), str(ezmap.map_name)))

        config = {
            "cache": {
                "name": "test",
                "path": "../tilestache/%s" % (request.user),
                "umask": "0000",
                "dirs": "portable"
            },
            "layers": {
                ezmap.map_name: {
                    "provider": {
                        "name":
                        "mapnik",
                        "mapfile":
                        "../tilestache/%s/%s.xml" %
                        (request.user, ezmap.map_name)
                    },
                    "projection": "spherical mercator"
                }
            }
        }

        # like http://tile.openstreetmap.org/1/0/0.png
        #coord = ModestMaps.Core.Coordinate(y, x, zoom)
        path = "/%s/%s/%s/%s.png" % (ezmap.map_name, zoom, x, y)
        config = TileStache.Config.buildConfiguration(config)
        #type, bytes = TileStache.getTile(config.layers[shapefile.filename], coord, 'png')
        type, bytes = TileStache.requestHandler(config, path)
        return HttpResponse(bytes, mimetype="image/png")

    except:
        traceback.print_exc()
        return HttpResponse("")
Example #36
0
def tileFeature(request, version, shapefile_id, feature_id, zoom, x, y):
    try:
        if version != "1.0":
            raise Http404
        try:
            shapefile = Shapefile.objects.get(id=shapefile_id, created_by=request.user)
        except Shapefile.DoesNotExist:
            raise Http404
        zoom = int(zoom)
        x = int(x)
        y = int(y)
        if zoom < 0 or zoom > MAX_ZOOM_LEVEL:
            raise Http404
        xExtent = _unitsPerPixel(zoom)
        yExtent = _unitsPerPixel(zoom)
        minLong = x * xExtent - 20037508.34
        minLat = y * yExtent - 20037508.34
        maxLong = minLong + xExtent
        maxLat = minLat + yExtent
        if (minLong < -20037508.34 or maxLong > 20037508.34 or minLat < -20037508.34 or maxLat > 20037508.34):
            raise Http404

        #create de mapnik.map object
        map = mapnik.Map(TILE_WIDTH, TILE_HEIGHT, "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over")
        map.background = mapnik.Color("#fff")
        #defining the feature layer
        geometryField = utils.calcGeometryField(shapefile.geom_type)
        query = '(select ' + geometryField +', id, id_relat as label from "layers_feature" WHERE shapefile_id = ' + str(shapefile.id) + ' AND id = ' + str(feature_id) + ') as geom'

        datasource = mapnik.PostGIS(user=dbSettings['USER'],
                        password=dbSettings['PASSWORD'],
                        dbname=dbSettings['NAME'],
                        port=dbSettings['PORT'],
                        table=query,
                        srid=3857,
                        geometry_field=geometryField,
                        simplify_geometries=True,
                        geometry_table='"layers_feature"')

        featureLayer = mapnik.Layer("featureLayer")
        featureLayer.srs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +no_defs +over"
        featureLayer.datasource = datasource
        featureLayer.styles.append("featureLayerStyle")

        #defining the feature layer styles
        rule = mapnik.Rule()
        if shapefile.geom_type in ["Point", "3D Point", "MultiPoint", "3D MultiPoint"]:
            rule.symbols.append(mapnik.PointSymbolizer())
        elif shapefile.geom_type in ["LineString", "3D LineString", "MultiLineString", "3D MultiLineString"]:
            rule.symbols.append(mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))
        elif shapefile.geom_type in ["Polygon", "3D Polygon", "MultiPolygon", "3D MultiPolygon"]:
            rule.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color("#f7edee")))
            rule.symbols.append(mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))
        style = mapnik.Style()
        style.rules.append(rule)
        map.append_style("featureLayerStyle", style)

        label_rule = mapnik.Rule()
        label = mapnik.TextSymbolizer(mapnik.Expression('[label]'), 'DejaVu Sans Book', 10, mapnik.Color('black'))
        label.halo_radius = 4
        label.allow_overlap = False
        label.avoid_edges = True
        label_rule.symbols.append(label)
        label_style = mapnik.Style()
        label_style.rules.append(label_rule)
        featureLayer.styles.append("featureLayerStyle_label")
        #add label to the map
        map.append_style("featureLayerStyle_label", label_style)


        #add new feature to the map
        map.layers.append(featureLayer)

        #rendering the map tile
        mapnik_xml_path = "../tilestache/%s/layers/vector/lightViewer/%s.xml" % (str(request.user), str(shapefile.name))
        mapnik.save_map(map, mapnik_xml_path)

        config = {
          "cache": {
            "name": "Test",
            "path": "../tilestache/%s" % (request.user),
            "umask": "0000",
            "dirs": "portable"},
          "layers": {
            shapefile.name: {
                "provider": {"name": "mapnik", "mapfile": mapnik_xml_path},
                "metatile":    {
                  "rows": 2,
                  "columns": 2,
                  "buffer": 64
                },
                "projection": "spherical mercator",
                "write cache": False
            }
          }
        }

        path = "/%s/%s/%s/%s.png" % (shapefile.name,zoom,x,y)
        config = TileStache.Config.buildConfiguration(config)
        type, bytes = TileStache.requestHandler(config, path)
        return HttpResponse(bytes, content_type="image/png")

    except:
        traceback.print_exc()
        return HttpResponse("")
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 #38
0
def tile(request, version, shapefile_id, zoom, x, y):
    """ Return a single Tile resource for our Tile Map Server.

        This returns the rendered map tile for a given zoom level, x and y
        coordinate.
    """
    try:
        # Parse the supplied parameters to see which area of the map to
        # generate.

        if version != "1.0":
            raise Http404

        shapefile = Shapefile.objects.get(id=shapefile_id)
        if shapefile == None:
            raise Http404

        geometryField = utils.calcGeometryField(shapefile.geom_type)
        geometryType  = utils.calcGeometryFieldType(shapefile.geom_type)

        zoom = int(zoom)
        x    = int(x)
        y    = int(y)

        if zoom < 0 or zoom > MAX_ZOOM_LEVEL:
            raise Http404

        xExtent = _unitsPerPixel(zoom) * TILE_WIDTH
        yExtent = _unitsPerPixel(zoom) * TILE_HEIGHT

        minLong = x * xExtent - 180.0
        minLat  = y * yExtent - 90.0
        maxLong = minLong + xExtent
        maxLat  = minLat  + yExtent

        if (minLong < -180 or maxLong > 180 or
            minLat < -90 or maxLat > 90):
            print "Map extent out of bounds:",minLong,minLat,maxLong,maxLat
            raise Http404

        # Prepare to display the map.

        map = mapnik.Map(TILE_WIDTH, TILE_HEIGHT,
                         "+proj=longlat +datum=WGS84")
        map.background = mapnik.Color("#7391ad")

        dbSettings = settings.DATABASES['default']

        # Setup our base layer, which displays the base map.

        datasource = mapnik.PostGIS(user=dbSettings['USER'],
                                    password=dbSettings['PASSWORD'],
                                    dbname=dbSettings['NAME'],
                                    table='"shapeEditor_basemap"',
                                    srid=4326,
                                    geometry_field="geometry",
                                    geometry_table='"shapeEditor_basemap"')

        baseLayer = mapnik.Layer("baseLayer")
        baseLayer.datasource = datasource
        baseLayer.styles.append("baseLayerStyle")

        rule = mapnik.Rule()

        rule.symbols.append(
            mapnik.PolygonSymbolizer(mapnik.Color("#b5d19c")))
        rule.symbols.append(
            mapnik.LineSymbolizer(mapnik.Color("#404040"), 0.2))

        style = mapnik.Style()
        style.rules.append(rule)

        map.append_style("baseLayerStyle", style)
        map.layers.append(baseLayer)

        # Setup our feature layer, which displays the features from the
        # shapefile.

        query = '(select ' + geometryField + ' from "shapeEditor_feature" where shapefile_id=' + str(shapefile.id) + ') as geom'

	print "QUERY: " + query +":" + geometryField+":" + geometryType

        datasource = mapnik.PostGIS(user=dbSettings['USER'],
                                    password=dbSettings['PASSWORD'],
                                    dbname=dbSettings['NAME'],
                                    table=query,
                                    srid=4326,
                                    geometry_field=geometryField,
                                    geometry_table='"shapeEditor_feature"')

	print "DATA SOURCE IS " + str(datasource)
        featureLayer = mapnik.Layer("featureLayer")
        featureLayer.datasource = datasource
        featureLayer.styles.append("featureLayerStyle")

        rule = mapnik.Rule()

        if geometryType in ["Point", "MultiPoint"]:
            rule.symbols.append(mapnik.PointSymbolizer())
        elif geometryType in ["LineString", "MultiLineString"]:
            rule.symbols.append(
                mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))
        elif geometryType in ["Polygon", "MultiPolygon"]:
            rule.symbols.append(
                mapnik.PolygonSymbolizer(mapnik.Color("#f7edee")))
            rule.symbols.append(
                mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5))

        style = mapnik.Style()
        style.rules.append(rule)

        map.append_style("featureLayerStyle", style)
        map.layers.append(featureLayer)

        # Finally, render the map.

        map.zoom_to_box(mapnik.Envelope(minLong, minLat, maxLong, maxLat))
        image = mapnik.Image(TILE_WIDTH, TILE_HEIGHT)
        mapnik.render(map, image)
        imageData = image.tostring('png')

        return HttpResponse(imageData, mimetype="image/png")
    except:
        traceback.print_exc()
        raise