Ejemplo n.º 1
0
    def post(self, request, *args, **kwargs):
        feature = None
        aoi = request.POST.get('aoi')
        geometry = request.POST.get('geometry')
        geojson = json.loads(geometry)
        properties = geojson.get('properties')

        aoi = AOI.objects.get(id=aoi)
        job = getattr(aoi, 'job')
        project = getattr(job, 'project')
        template = properties.get('template') if properties else None

        # TODO: handle exceptions
        if template:
            template = FeatureType.objects.get(id=template)

        attrs = dict(aoi=aoi,
                     job=job,
                     project=project,
                     analyst=request.user,
                     template=template)

        geometry = geojson.get('geometry')
        attrs['the_geom'] = GEOSGeometry(json.dumps(geometry))

        try:
            response = Feature(**attrs)
            response.full_clean()
            response.save()
        except ValidationError as e:
            return HttpResponse(content=json.dumps(dict(errors=e.messages)),
                                mimetype="application/json",
                                status=400)

        return HttpResponse([response], mimetype="application/json")
Ejemplo n.º 2
0
def add_feature(data, user):
    feature = Feature()
    feature.name = data[NAME]
    feature.group = data[GROUP]
    feature.updated_by = user
    feature.save()
    return feature
Ejemplo n.º 3
0
Archivo: views.py Proyecto: Carl4/geoq
    def post(self, request, *args, **kwargs):
        feature = None
        aoi = request.POST.get('aoi')
        geometry = request.POST.get('geometry')
        geojson = json.loads(geometry)
        properties = geojson.get('properties')

        aoi = AOI.objects.get(id=aoi)
        job = getattr(aoi, 'job')
        project = getattr(job, 'project')
        template = properties.get('template') if properties else None

        #TODO: handle exceptions
        if template:
            template = FeatureType.objects.get(id=template)

        attrs = dict(aoi=aoi,
                     job=job,
                     project=project,
                     analyst=request.user,
                     template=template)

        geometry = geojson.get('geometry')
        attrs['the_geom'] = GEOSGeometry(json.dumps(geometry))

        try:
            response = Feature(**attrs)
            response.full_clean()
            response.save()
        except ValidationError as e:
            return HttpResponse(content=json.dumps(dict(errors=e.messages)), mimetype="application/json", status=400)

        return HttpResponse([response], mimetype="application/json")
Ejemplo n.º 4
0
def vector_catalog_save_layer(tenant, layer, vector_layer, features):
    connection.close()
    connection.set_schema(tenant)

    features = VECTOR_LAYERS[vector_layer]['geometries_by_id'](features)

    with transaction.atomic():
        union = GEOSGeometry('POINT EMPTY')
        keys = None
        for g, props in features:
            if not keys:
                keys = props.keys()

            union = union.union(g)
            g.transform(3857)

            s = hashlib.sha1()
            s.update(GeometryCollection(g).ewkb)
            props['shaid'] = s.hexdigest()
            f = Feature(layer=layer,
                        geometry=GeometryCollection(g),
                        properties=props)
            f.save()

        envelope = union.envelope.coords[0]
        layer.bounds = envelope[2] + envelope[0]
        layer.status = 0
        layer.field_names = list(set(layer.field_names).union(set(keys)))
        layer.schema['properties'] = {n: "str" for n in layer.field_names}
        layer.save()
Ejemplo n.º 5
0
    def post(self, request, *args, **kwargs):
        feature = None
        tpi = request.META.get('HTTP_TEMP_POINT_ID', "none")
        aoi = request.POST.get('aoi')
        geometry = request.POST.get('geometry')
        geojson = json.loads(geometry)
        properties = geojson.get('properties')

        aoi = AOI.objects.get(id=aoi)
        job = getattr(aoi, 'job')
        project = getattr(job, 'project')
        template = properties.get('template') if properties else None

        # TODO: handle exceptions
        if template:
            template = FeatureType.objects.get(id=template)

        attrs = dict(aoi=aoi,
                     job=job,
                     project=project,
                     analyst=request.user,
                     template=template)

        geometry = geojson.get('geometry')
        geom_obj = GEOSGeometry(json.dumps(geometry))
        attrs['the_geom'] = geom_obj

        county_list = Counties.objects.filter(
            poly__contains=geom_obj.centroid.wkt)
        county = None
        if len(county_list):
            county = str(county_list[0].name)

        try:
            feature = Feature(**attrs)
            feature.full_clean()
            if not feature.properties:
                feature.properties = {}
            if county:
                feature.properties['county'] = county

            feature.save()
        except ValidationError as e:
            response = HttpResponse(content=json.dumps(
                dict(errors=e.messages)),
                                    mimetype="application/json",
                                    status=400)
            response['Temp-Point-Id'] = tpi
            return response
        # This feels a bit ugly but it does get the GeoJSON into the response
        feature_json = serializers.serialize('json', [
            feature,
        ])
        feature_list = json.loads(feature_json)
        feature_list[0]['geojson'] = feature.geoJSON(True)

        response = HttpResponse(json.dumps(feature_list),
                                mimetype="application/json")
        response['Temp-Point-Id'] = tpi
        return response
Ejemplo n.º 6
0
 def save(self, json_str):
     json_obj = json.loads(json_str)
     feature_name = json_obj['feature']['feature_name']
     feature_desc = json_obj['feature']['feature_description']
     feature_save = Feature().fill(feature_name, feature_desc, '', '')
     feature_save.save()
     # scenarios = json_obj['feature']['scenarios']
     # for sce in scenarios:
     #     self.CommonSaver.save_new_scenario(feature_save, sce)
     return feature_save
Ejemplo n.º 7
0
    def post(self, request, *args, **kwargs):
        feature = None
        tpi = request.META.get('HTTP_TEMP_POINT_ID', "none")
        aoi = request.POST.get('aoi')
        geometry = request.POST.get('geometry')
        geojson = json.loads(geometry)
        properties = geojson.get('properties')

        aoi = AOI.objects.get(id=aoi)
        job = getattr(aoi, 'job')
        project = getattr(job, 'project')
        template = properties.get('template') if properties else None

        # TODO: handle exceptions
        if template:
            template = FeatureType.objects.get(id=template)

        attrs = dict(aoi=aoi,
                     job=job,
                     project=project,
                     analyst=request.user,
                     template=template)

        geometry = geojson.get('geometry')
        geom_obj = GEOSGeometry(json.dumps(geometry))
        attrs['the_geom'] = geom_obj

        county_list = Counties.objects.filter(poly__contains=geom_obj.centroid.wkt)
        county = None
        if len(county_list):
            county = str(county_list[0].name)

        try:
            feature = Feature(**attrs)
            feature.full_clean()
            if not feature.properties:
                feature.properties = {}
            if county:
                feature.properties['county'] = county

            feature.save()
        except ValidationError as e:
            response =  HttpResponse(content=json.dumps(dict(errors=e.messages)), mimetype="application/json", status=400)
            response['Temp-Point-Id'] = tpi
            return response
        # This feels a bit ugly but it does get the GeoJSON into the response
        feature_json = serializers.serialize('json', [feature,])
        feature_list = json.loads(feature_json)
        feature_list[0]['geojson'] = feature.geoJSON(True)

        response = HttpResponse(json.dumps(feature_list), mimetype="application/json")
        response['Temp-Point-Id'] = tpi
        return response
Ejemplo n.º 8
0
    def test06_geometryfield(self):
        "Testing GeometryField."
        f1 = Feature(name='Point', geom=Point(1, 1))
        f2 = Feature(name='LineString', geom=LineString((0, 0), (1, 1), (5, 5)))
        f3 = Feature(name='Polygon', geom=Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0))))
        f4 = Feature(name='GeometryCollection', 
                     geom=GeometryCollection(Point(2, 2), LineString((0, 0), (2, 2)), 
                                             Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0)))))
        f1.save()
        f2.save()
        f3.save()
        f4.save()

        f_1 = Feature.objects.get(name='Point')
        self.assertEqual(True, isinstance(f_1.geom, Point))
        self.assertEqual((1.0, 1.0), f_1.geom.tuple)
        f_2 = Feature.objects.get(name='LineString')
        self.assertEqual(True, isinstance(f_2.geom, LineString))
        self.assertEqual(((0.0, 0.0), (1.0, 1.0), (5.0, 5.0)), f_2.geom.tuple)

        f_3 = Feature.objects.get(name='Polygon')
        self.assertEqual(True, isinstance(f_3.geom, Polygon))
        f_4 = Feature.objects.get(name='GeometryCollection')
        self.assertEqual(True, isinstance(f_4.geom, GeometryCollection))
        self.assertEqual(f_3.geom, f_4.geom[2])
Ejemplo n.º 9
0
    def test06_geometryfield(self):
        "Testing GeometryField."
        f1 = Feature(name='Point', geom=Point(1, 1))
        f2 = Feature(name='LineString',
                     geom=LineString((0, 0), (1, 1), (5, 5)))
        f3 = Feature(name='Polygon',
                     geom=Polygon(
                         LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0))))
        f4 = Feature(name='GeometryCollection',
                     geom=GeometryCollection(
                         Point(2, 2), LineString((0, 0), (2, 2)),
                         Polygon(
                             LinearRing((0, 0), (0, 5), (5, 5), (5, 0),
                                        (0, 0)))))
        f1.save()
        f2.save()
        f3.save()
        f4.save()

        f_1 = Feature.objects.get(name='Point')
        self.assertEqual(True, isinstance(f_1.geom, Point))
        self.assertEqual((1.0, 1.0), f_1.geom.tuple)
        f_2 = Feature.objects.get(name='LineString')
        self.assertEqual(True, isinstance(f_2.geom, LineString))
        self.assertEqual(((0.0, 0.0), (1.0, 1.0), (5.0, 5.0)), f_2.geom.tuple)

        f_3 = Feature.objects.get(name='Polygon')
        self.assertEqual(True, isinstance(f_3.geom, Polygon))
        f_4 = Feature.objects.get(name='GeometryCollection')
        self.assertEqual(True, isinstance(f_4.geom, GeometryCollection))
        self.assertEqual(f_3.geom, f_4.geom[2])
Ejemplo n.º 10
0
    def post(self, request, *args, **kwargs):
        feature = None
        aoi = request.POST.get('aoi')
        geometry = request.POST.get('geometry')
        geojson = json.loads(geometry)
        properties = geojson.get('properties')

        aoi = AOI.objects.get(id=aoi)
        job = getattr(aoi, 'job')
        project = getattr(job, 'project')
        template = properties.get('template') if properties else None

        # TODO: handle exceptions
        if template:
            template = FeatureType.objects.get(id=template)

        attrs = dict(aoi=aoi,
                     job=job,
                     project=project,
                     analyst=request.user,
                     template=template)

        geometry = geojson.get('geometry')
        attrs['the_geom'] = GEOSGeometry(json.dumps(geometry))

        try:
            feature = Feature(**attrs)
            feature.full_clean()
            feature.save()
        except ValidationError as e:
            return HttpResponse(content=json.dumps(dict(errors=e.messages)),
                                mimetype="application/json",
                                status=400)

        # This feels a bit ugly but it does get the GeoJSON into the response
        feature_json = serializers.serialize('json', [
            feature,
        ])
        feature_list = json.loads(feature_json)
        feature_list[0]['geojson'] = feature.geoJSON(True)

        return HttpResponse(json.dumps(feature_list),
                            mimetype="application/json")
Ejemplo n.º 11
0
Archivo: views.py Proyecto: Bauerr/geoq
    def post(self, request, *args, **kwargs):
        feature = None
        aoi = request.POST.get('aoi')
        geometry = request.POST.get('geometry')
        geojson = json.loads(geometry)
        properties = geojson.get('properties')

        aoi = AOI.objects.get(id=aoi)
        job = getattr(aoi, 'job')
        project = getattr(job, 'project')
        template = properties.get('template') if properties else None

        # TODO: handle exceptions
        if template:
            template = FeatureType.objects.get(id=template)

        attrs = dict(aoi=aoi,
                     job=job,
                     project=project,
                     analyst=request.user,
                     template=template)

        geometry = geojson.get('geometry')
        attrs['the_geom'] = GEOSGeometry(json.dumps(geometry))

        try:
            feature = Feature(**attrs)
            feature.full_clean()
            feature.save()
        except ValidationError as e:
            return HttpResponse(content=json.dumps(dict(errors=e.messages)), mimetype="application/json", status=400)

        # This feels a bit ugly but it does get the GeoJSON into the response
        feature_json = serializers.serialize('json', [feature,])
        feature_list = json.loads(feature_json)
        feature_list[0]['geojson'] = feature.geoJSON(True)

        return HttpResponse(json.dumps(feature_list), mimetype="application/json")
Ejemplo n.º 12
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!!")
Ejemplo n.º 13
0
def save_features(request, strain_id):
    """
    seqid = line[0]
    source = line[1]
    type = line[2]
    start = line[3]
    end = line[4]
    score = line[5]
    strand = line[6] # "+", "-", or "."
    phase = line[7]
    attributes = line[8]
    """
    theStrain = Strain.objects.get(pk = strain_id)
    gffFile = request.FILES['gff_file']
    gff = gffFile.read()
    gff = gff.split('###')[0] # Throw away the sequence
    gff = [x.split('\t') for x in gff.splitlines() if x[0] != '#'] # Throw away the header comments. Now we're left with just the meat of the file

    contigMap = {}
    for seqid, source, featureType, start, end, score, strand, phase, attributes in gff:
        attributeParts = attributes.split(';')
        attributeParts = [x.split('=') for x in attributeParts]
        attributeParts = [(x[0], x[1].split(',')) for x in attributeParts]
        attributeParts = [(x[0], [urllib.unquote(y) for y in x[1]]) for x in attributeParts]

        attributeDict = {}
        for key, value in attributeParts:
            attributeDict[key] = value

        if featureType == 'contig':
            # We need to add this to the contigMap
            try:
                contigName = attributeDict['dbxref'][0].split(':')[-1]
            except KeyError:
                contigName = attributeDict['ID'][0]
            contigMap[seqid] = contigName
        elif featureType == 'chromosome':
            contigMap[seqid] = seqid
        else: # This is an actual feature line. It is assumed that we have already gone through all the contig lines
            theContig = get_object_or_404(Contig, name=contigMap[seqid] ) # Get the Contig we're going to point to
            feature = Feature()
            feature.contig = theContig
            try:
                feature.feature_id = attributeDict['ID'][0]
            except KeyError:
                pass
            else:
                # This one has a name that might be found in the Reference table
                if feature.feature_id.find(theStrain.name) != -1:
                    # Yup, it's one we need to link to the Reference table
                    referenceName = feature.feature_id.split("_")[0]
                    feature.reference = Reference.objects.get(feature_name = referenceName)
                else: # just try and see if there is a reference with this unmodified feature_id
                    try:
                        feature.reference = Reference.objects.get(feature_name = feature.feature_id)
                    except ObjectDoesNotExist:
                        pass

            if 'Parent' in attributeDict:
                parent = get_object_or_404(Feature, feature_id = attributeDict['Parent'][0], contig = theContig)
                feature.parent = parent
            feature.feature_type = featureType
            feature.start_coord = int(start)
            feature.stop_coord = int(end)
            feature.strand = strand

            feature.createdDate = datetime.datetime.now()
            feature.modifiedDate = datetime.datetime.now()

            feature.save()
    return HttpResponseRedirect('/strains/')