Exemple #1
0
 def add_polygon(geom_dict):
     try:
         geom = GEOSGeometry(json.dumps(geom_dict), 4326)
     except GEOSException:
         raise ValidationError('GeoJSON is not valid')
     geom.transform(web_mercator)
     geoms.append(geom)
Exemple #2
0
 def test_transform_3d(self):
     p3d = GEOSGeometry('POINT (5 23 100)', 4326)
     p3d.transform(2774)
     if GEOS_PREPARE:
         self.assertEqual(p3d.z, 100)
     else:
         self.assertIsNone(p3d.z)
Exemple #3
0
def loadAreaContent(request, zoom, gridSize):

    clusterer = MapClusterer(zoom, gridSize)

    params = clusterer.loadJson(request)

    filterstring = clusterer.constructFilterstring(params["filters"])

    geojson = params["geojson"]

    markers = []

    if geojson["type"] == "Feature":
        features = [params["geojson"]]

    elif geojson["type"] == "FeatureCollection":
        features = geojson["features"]
        
        
    for feature in features:
        geometry = GEOSGeometry(json.dumps(feature["geometry"]), srid=clusterer.input_srid)
        geometry.transform(clusterer.srid_db)
        markers_qry = Gis.objects.raw(
            '''SELECT * FROM "%s" WHERE ST_Intersects(%s, ST_GeomFromText('%s',%s) ) %s;''' % (geo_table, geo_column_str, geometry, clusterer.srid_db, filterstring)
        )

        markers += list(markers_qry)
    

    return markers
Exemple #4
0
def bbox_to_projection(native_bbox, target_srid=4326):
    """
        native_bbox must be in the form
            ('-81.3962935', '-81.3490249', '13.3202891', '13.3859614', 'EPSG:4326')
    """
    box = native_bbox[:4]
    proj = native_bbox[-1]
    minx, maxx, miny, maxy = [float(a) for a in box]
    try:
        source_srid = int(proj.split(":")[1]) if proj and ':' in proj else int(proj)
    except BaseException:
        source_srid = target_srid

    if source_srid != target_srid:
        try:
            wkt = bbox_to_wkt(_v(minx, x=True, source_srid=source_srid, target_srid=target_srid),
                              _v(maxx, x=True, source_srid=source_srid, target_srid=target_srid),
                              _v(miny, x=False, source_srid=source_srid, target_srid=target_srid),
                              _v(maxy, x=False, source_srid=source_srid, target_srid=target_srid),
                              srid=source_srid)
            poly = GEOSGeometry(wkt, srid=source_srid)
            poly.transform(target_srid)
            projected_bbox = [str(x) for x in poly.extent]
            # Must be in the form : [x0, x1, y0, y1, EPSG:<target_srid>)
            return tuple([projected_bbox[0], projected_bbox[2], projected_bbox[1], projected_bbox[3]]) + \
                ("EPSG:%s" % poly.srid,)
        except BaseException:
            tb = traceback.format_exc()
            logger.debug(tb)

    return native_bbox
Exemple #5
0
    def handle(self, *args, **options):
        from sigeo.obcine.models import Obcina
        import csv
        from django.contrib.gis.geos import GEOSGeometry
        from sigeo.preprocessing import get_coordtransform
        import StringIO

        s = StringIO.StringIO()
        w = csv.writer(s)
        trans = get_coordtransform()
        w.writerow(['id', 'ime', 'uime', 'tip', 'povrsina', 'center', 'geometrija'])

        for ob in Obcina.objects.all():
            center_pt = 'SRID=3787;POINT(%d %d)' % (ob.y_c, ob.x_c)
            pt = GEOSGeometry(center_pt)
            pt.transform(trans)

            row = [
                ob.ob_id,
                ob.ob_ime,
                ob.ob_uime,
                ob.ob_tip,
                ob.ob_pov,
                pt.kml,
                ob.the_geom.kml,
            ]
            w.writerow([unicode(i).encode('utf-8') for i in row])

        print s.getvalue()
Exemple #6
0
def gk_to_wgs84(request):

    coords = map(request.GET.get, ["x", "y"])

    if not all(coords):
        return {"status": "fail", "error": "Missing coordinates x and y as GET parameters."}

    try:
        coords = map(float, coords)
    except ValueError:
        return {"status": "fail", "error": "Coordinates should be floats."}

    xl, xh, yl, yh = 372543, 631496, 34152, 197602
    if not (xl <= coords[0] <= xh and yl <= coords[1] <= yh):
        return {
            "status": "fail",
            "error": "Coordinates (%s, %s) out of bounds: %d <= x <= %d and %d <= y <= %d."
            % (coords[0], coords[1], xl, xh, yl, yh),
        }

    geotransform = get_coordtransform()
    point = GEOSGeometry("SRID=3787;POINT (%s %s)" % tuple(coords))
    point.transform(geotransform)
    transformed = (point.x, point.y)

    return {"status": "ok", "gk": coords, "wgs84": transformed, "kml": point.kml}
Exemple #7
0
def place(request):
    """
    Return the 900913 coordinates for the given address.
    Principally used to obtain the coords for a given address
    to centre the map with.
    """
    import geopy
    from django.contrib.gis.geos import GEOSGeometry

    places = geopy.geocoders.Google().geocode(request.GET.get('address'), exactly_one=False)

    dplaces = {}
    for p in places:
        P = 'POINT(' + str(p[1][1]) + ' ' + str(p[1][0]) + ')'
        # SRID 4326 is the common latitude/longitude coords
        pnt = GEOSGeometry(P, srid=4326)
        pnt.transform(900913)
        dplaces[p[0]] = pnt

    return HttpResponse(simplejson.dumps(dict(
        (key, {
            'lat': value.x,
            'lng': value.y,
        }) for key, value in dplaces.iteritems())
    ), mimetype='application/json')
Exemple #8
0
def parse_sos112(timestamp, data):
    data = data.decode(ENCODING).strip()

    records = []
    lines = [i.strip() for i in data.split('\r\n')]
    keys = lines[0].lower().split('\t')

    geotransform = get_coordtransform()

    for line in lines[1:]:
        rec = dict(zip(keys, line.split('\t')))
        y, x = rec['point'].split(',')

        point = GEOSGeometry('SRID=3787;POINT (%s %s)' % (x, y))
        point.transform(geotransform)
        rec['x_wgs'] = point.x
        rec['y_wgs'] = point.y

        records.append(rec)

    json_data = {
        'updated': timestamp,
        'records': records,
        'copyright': u'Uprava RS za zaščito in reševanje',
    }

    return json_data
Exemple #9
0
 def from_json(self, data):
     try:
         try:
             geom = GEOSGeometry(str(data.geometry))
             if(hasattr(data.geometry.crs, 'properties')):
                 crs = data.geometry.crs.properties['name']
                 srs = SpatialReference(crs)
                 geom.set_srid(srs.srid)
                 geom.transform(4326)
             ls = LineString(geom[0].coords)
             if(ls.simple == False):
                 return None, 'Error Creating Geometry: Polygon is not Valid'
             self.geom = geom
         except:
             logger.debug(sys.exc_info())
             return None, 'Error Creating Geometry'
         if('name' in data.__dict__['properties']):
             self.name = data.__dict__['properties']['name']
         else:
             return None, 'Name is required'
         if('max_area' in data.__dict__['properties']):
             try:
                 self.max_area = int(data.__dict__['properties']['max_area'])
             except ValueError:
                 return None, 'Invalid Max Area'
         else:
             return None, 'Max Area is Required'
         self.save()
         return self, None
     except:
         # ToDo catch errors specifically and return message/code
         return None, 'Unknown'
Exemple #10
0
 def from_json(self, data):
     try:
         try:
             geom = GEOSGeometry(str(data.geometry))
             if(hasattr(data.geometry.crs, 'properties')):
                 crs = data.geometry.crs.properties['name']
                 srs = SpatialReference(crs)
                 geom.set_srid(srs.srid)
                 geom.transform(4326)
             self.geom = geom
         except:
             return None, "Invalid Geometry"
         if('project_id' in data.__dict__['properties']):
             try:
                 self.project = Project.objects.get(id=int(data.__dict__['properties']['project_id']))
             except (ValueError, ObjectDoesNotExist):
                 return None, "Invalid Project"
         else:
             return None, "Project is required"
         if('name' in data.__dict__['properties']):
             self.name = data.__dict__['properties']['name']
         else:
             return None, "Name is Required"
         self.save()
         return self, None
     except:
         return None, "Unexpected Error"
    def to_geojson(self, data, options=None):
        """
        Given some Python data, produces GeoJSON output.
        """
        options = options or {}
        data = self.to_simple(data, options)

        if 'objects' in data:
            data['type'] = "FeatureCollection"
            data['features'] = data['objects']
            del data['objects']

            for index, obj in enumerate(data['features']):
                for key, value in obj.items():

                    if hasattr(value, 'lower') and (value.lower().split('(')[0].strip() in ('point', 'multipoint', 'linestring','multilinestring','polygon','multipolygon')):
                        if options['srid']:
                            srid = options['srid']
                            try:
                                srid=int(srid)
                            except ValueError:
                                pass

                            geometry = GEOSGeometry(value)
                            geometry.transform(srid)
                            geometry = simplejson.loads(geometry.geojson)
                        else:
                            geometry = simplejson.loads(GEOSGeometry(value).geojson)
                        
                        del obj[key]
                        geojson = { 'geometry' : geometry, 'properties' : obj, 'type' : "Feature" }
                        data['features'][index] = geojson

        return simplejson.dumps(data, cls=json.DjangoJSONEncoder, sort_keys=True)
Exemple #12
0
    def post(self, request, *args, **kwargs):
        if request.is_ajax():
            cur_shp_id = kwargs['pk']

            # calculate area as GEOS object using convex hull operation,
            # save it, calculate area value, save it in db as geometry attribute
            cur_shp = Shapefile.objects.get(id=cur_shp_id)
            cur_shp_geom = get_geos_geometry(cur_shp)
            area_geom = GEOSGeometry(cur_shp_geom.convex_hull)

            # calculates the area and update the db entry
            area_geom.set_srid(4326)  # get measure in Google Mercator proj
            area_geom.transform(3857)  # ibidem

            # get area value in Shapefile's projection
            proj_area_geom = area_geom
            proj_area_geom.transform(cur_shp.proj)

            # save the new GEOS area to geometry in db
            new_area = HelperSettlementArea(shapefile_id=cur_shp_id,
                                            poly=area_geom,
                                            storedarea=proj_area_geom.area)
            new_area.save()

            cur_shp.stat_sett_area = True
            cur_shp.save()

            context = context_results(cur_shp_id)
            return render_to_response(
                'appgeostat/shp_detail_table.html', {'context': context})
def add_geometry_database(table_name, geometry_data, cursor, conn):
    """
    Add the geometry into the database
    """
    # Create table if not exist for the layer
    cursor.execute("""CREATE TABLE IF NOT EXISTS {0} (id serial PRIMARY KEY,
                      geometry geometry(Geometry,3857) NOT NULL,
                      geometry_type varchar(40) NOT NULL)""".format(table_name))

    # Add geometry and geometry type of the geojson into the database
    for feature in range(len(geometry_data['features'])):
        geometry = geometry_data['features'][feature]['geometry']
        geometry_type = geometry['type']

        # Convert geojson into geometry
        geojson = GEOSGeometry(str(geometry), srid=4326)
        geojson.transform(3857)
        geom = geojson.hex.decode()

        # Add the geometry into the table if the geometry doesn't already exist
        cursor.execute("""INSERT INTO {0}(geometry, geometry_type)
                          SELECT ST_SetSRID(\'{1}\'::geometry, 3857) AS geometry,
                                 \'{2}\' AS geometry_typ
                          WHERE NOT EXISTS
                              (SELECT geometry
                               FROM {0}
                               WHERE geometry = ST_SetSRID(\'{1}\'::geometry, 3857))
                       """.format(table_name, geom, geometry_type))

    # Save changes
    conn.commit()
Exemple #14
0
 def feature_query(self, query, parameters = {}):
     """
     Transforms a SQL query into a GeoJSON-formatted FeatureColection.
     It is possible to define a source SRS and target SRS if necessary
     by passing the keys 'source_srs' and 'target_srs' to the parameters
     dictionary. The SRS can be specified using a ESPG code integer value,
     or a PROJ.4 projection string.
     """
     def printDecimal(d):
         n = 3 # Number of decimal places
         s = str(d)
         s = s[:s.find('.') + 1 + n]
         return '|' + s + '|'
     # Define source and target SRS. If the source SRS is None (default), 
     # it is defined by the data source itself.
     s_srs = None
     if parameters.has_key('source_srs'):
         s_srs = parameters['source_srs']
     # Default target SRID is Google Mercator (EPSG 900913)
     t_srs = '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs'
     if parameters.has_key('target_srs'):
         t_srs = parameters['target_srs']
     cur = self.connection.cursor()
     cur.execute(query)
     cols = cur.description
     id_col = -1
     geometry_col = -1
     for i in range(0, len(cols)):
         if cols[i][0] == 'id':
             id_col = i
         if cols[i][0] == 'geometry':
             geometry_col = i
     result = {}
     result['type'] = 'FeatureCollection'
     features = []
     for m in cur:
         feature = {}
         feature['type'] = 'Feature'
         if geometry_col > -1:
             if s_srs == None:
                 geometry = GEOSGeometry(m[geometry_col])
             else:
                 geometry = GEOSGeometry(m[geometry_col], s_srs)
             # Project if necessary
             if s_srs != t_srs:
                 geometry.transform(t_srs)
             feature['geometry'] = json.loads(geometry.json, parse_float=decimal.Decimal)
         if id_col > -1: feature['id'] = m[id_col]
         properties = {}
         for i in range(0, len(cols)):
             if i != geometry_col: properties[cols[i][0]] = str(m[i]) # this throws a UnicodeEncodeError with unicode strings
         feature['properties'] = properties
         features.append(feature)
     result['features'] = features
     properties = {}
     properties['count'] = len(features)
     result['properties'] = properties
     cur.close()
     return result
Exemple #15
0
 def run(self):
     try:
         g = GEOSGeometry('SRID=4326;POINT(%s %s)' % (self.input_lon, self.input_lat))
         g.transform(settings.GEOMETRY_DB_SRID)
         self.output_point_geom = g
         self.output_poly_geom = g.buffer(self.input_buffer_distance)
         self.output_area = self.output_poly_geom.area
     except:
         return False
     return True
Exemple #16
0
def _transform_dataset(original_data):
    data = _loads(original_data)
    geotransform = get_coordtransform()
    for pr in data:
        si_point = GEOSGeometry('SRID=3787;POINT (%s %s)' % (pr['x'], pr['y']))
        si_point.transform(geotransform)
        pr['x_wgs'] = si_point.x
        pr['y_wgs'] = si_point.y

    return data
Exemple #17
0
def area(request):
	if request.is_ajax():
		#print request.body
		#q = json.loads(request.body)
		poligono= GEOSGeometry(request.body,srid=900913)
		poligono.transform(4326)
		print poligono.coords
		area= poligono.area
		print area
	return HttpResponse(area,mimetype='application/json')
# Create your views here.
    def gridCluster(self, request):

        params = self.loadJson(request)

        clusterGeometries = self.getClusterGeometries(request, params, "viewport")

        gridCells = []

        if clusterGeometries:

            filterstring = self.constructFilterstring(params["filters"])

            cursor = connections['default'].cursor()

            cursor.execute('''CREATE TEMPORARY TABLE temp_clusterareas (
                  id serial,
                  polygon geometry
               )''')

            for clusterGeometry in clusterGeometries:
                cursor.execute('''
                    INSERT INTO temp_clusterareas (polygon)
                    ( SELECT (
                        ST_Dump(
                            ST_GeometryFromText('%s')
                    )).geom )
                ''' % clusterGeometry["geos"].ewkt)

            # indexing did not increase performance
            # cursor.execute('''CREATE INDEX temp_gix ON temp_clusterareas USING GIST (polygon);''')

            gridcluster_queryset = '''
                SELECT count(*) AS count, polygon FROM "%s", temp_clusterareas
                WHERE coordinates IS NOT NULL AND ST_Intersects(coordinates, polygon) %s
                GROUP BY polygon
            ''' % (geo_table, filterstring)

            cursor.execute(gridcluster_queryset)            

            gridCells_pre = cursor.fetchall()

            for cell in gridCells_pre:

                count = cell[0]

                geos = GEOSGeometry(cell[1])
                geos.transform(self.input_srid)
                centroid = geos.centroid
                
                cellobj = {"count":count, "geojson": geos.geojson, "center":{"x":centroid.x, "y": centroid.y}}

                gridCells.append(cellobj)
                
        return gridCells
Exemple #19
0
def parse_parkirisca_lpt(parkirisca_data, occupancy_data):

    # silly xmlns
    parkirisca_data = parkirisca_data.replace(' xmlns="http://www.tempuri.org/dsP.xsd"', '')

    parkirisca = ET.fromstring(parkirisca_data)
    occupancy = ET.fromstring(occupancy_data)

    zattrs = ['ID_ParkiriscaNC', 'Cas', 'P_kratkotrajniki']
    zattrs.sort()
    zasedenost = {}

    for e in occupancy.findall('./ZASEDENOST'):
        zdict = dict([(i.tag, i.text) for i in e.getchildren()])
        assert list(sorted(zdict.keys())) == zattrs, 'occupancy.xml attributes changed!'
        zdict['Cas_timestamp'] = int(time.mktime(datetime.datetime.strptime(zdict['Cas'], '%Y-%m-%d %H:%M:%S').timetuple()))
        for k, v in zdict.items():
            if isinstance(v, str) and re.match('^\d+$', v):
                zdict[k] = int(v)
        zasedenost[zdict['ID_ParkiriscaNC']] = zdict

    assert len(zasedenost) > 1, 'Ni elementov v occupancy.xml?!'

    json = {'Parkirisca': [],}

    attrs = ['A_St_Mest', 'Cena_dan_Eur', 'Cena_mesecna_Eur', 'Cena_splosno', 'Cena_ura_Eur', 'ID_Parkirisca', 'ID_ParkiriscaNC', 'Ime', 'Invalidi_St_mest', 'KoordinataX', 'KoordinataY', 'Opis', 'St_mest', 'Tip_parkirisca', 'U_delovnik', 'U_sobota', 'U_splosno', 'Upravljalec']
    attrs.sort()
    geotransform = get_coordtransform()

    for p in parkirisca.findall('.//Parkirisca'):
        pdict = dict([(i.tag, i.text) for i in p.getchildren()])

        for k, v in pdict.items():
            if isinstance(v, basestring) and re.match('^\d+$', v):
                pdict[k] = int(v)

        assert list(sorted(pdict.keys())) == attrs, "parkirisca.xml Attributes changed!?"
        if zasedenost.get(pdict['ID_ParkiriscaNC']) != None:
            pdict['zasedenost'] = zasedenost[pdict['ID_ParkiriscaNC']]

        # convert coords to WGS84
        if pdict['KoordinataX'] and pdict['KoordinataY']:
            si_point = GEOSGeometry('SRID=3787;POINT (%s %s)' % (pdict['KoordinataX'], pdict['KoordinataY']))
            si_point.transform(geotransform)
            pdict[u'KoordinataX_wgs'] = si_point.x
            pdict[u'KoordinataY_wgs'] = si_point.y

        json['Parkirisca'].append(pdict)

    json['updated'] = time.time()
    json['copyright'] = COPYRIGHT_LPT
    return json
    def manipulate(self):
        #extract target_shape geometry
        target_shape = self.target_to_valid_geom(self.target_shape)

        #extract study_region geometry
        #study_region argument is FOR UNIT-TESTING PURPOSES ONLY, otherwise we access the database
        if self.study_region is not None:
            try:
                study_region = GEOSGeometry(self.study_region)
                study_region.set_srid(settings.GEOMETRY_CLIENT_SRID)
                study_region.transform(settings.GEOMETRY_DB_SRID)
            except Exception, e:
                raise self.InternalException("Exception raised in ClipToStudyRegionManipulator while initializing study region geometry: " + e.message)
Exemple #21
0
def get_geos(value, srid=DEFAULT_PROJ):
    geos = None
    if value:
        if isinstance(value, GEOSGeometry):
            geos = value
        elif isinstance(value, six.text_type):
            match = _ewkt_re.match(value)
            if match:
                geos = GEOSGeometry(match.group('wkt'), match.group('srid'))
            else:
                geos = GEOSGeometry(value, srid)
    if geos and geos.srid and int(srid) != geos.srid:
        geos.transform(int(srid))
    return geos
Exemple #22
0
 def testComputeLookAt(self):
     """
     Check computing of lookat values
     """
     g1 = GEOSGeometry(
         'SRID=4326;MULTIPOLYGON(((-120.42 34.37, -119.64 34.32, -119.63 34.12, -120.44 34.15, -120.42 34.37)))')
     g1.transform(settings.GEOMETRY_DB_SRID)
     region = StudyRegion.objects.create(geometry=g1, name="Test region", active=True)
     region.lookAt_Lat = 0
     region.lookAt_Lon = 0
     self.assertEquals(region.lookAt_Lat, 0.0)
     self.assertEquals(region.lookAt_Lon, 0.0)
     lookat_kml = region.lookAtKml()
     self.assertAlmostEquals(region.lookAt_Lat, 34.239691894000003)
     self.assertAlmostEquals(region.lookAt_Lon, -120.03929305)
Exemple #23
0
    def from_native(self, value):
        import json
        geom = GEOSGeometry(json.dumps(value))
        srid = getattr(settings, 'SRID', 4326)

        if 'crs' in value and value['crs'].get('type', None) == 'name':
            name = value['crs']['properties']['name']
            if name.startswith('urn:ogc:def:crs:EPSG::'):
                geom.srid = int(name.replace('urn:ogc:def:crs:EPSG::', ''))

        if geom.srid is None:
            geom.srid = 4326
        if geom.srid != srid:
            geom.transform(srid)
        return geom
def create_geometry_dict(item):
    """
    Creates a geometry dict that can be used to add
    geometry information to the result set

    Returns a dict with geometry information if one
    can be created. If not, an empty dict is returned
    """
    res = {}
    try:
        geom_wgs = GEOSGeometry(
            f"POINT ({item['centroid'][0]} {item['centroid'][1]}) ", srid=4326)
    except(AttributeError, KeyError):
        geom_wgs = None

    if geom_wgs:
        # Convert to wgs
        geom = geom_wgs.transform(28992, clone=True).coords
        geom_wgs = geom_wgs.coords
        res = {
            'geometrie_rd_x': int(geom[0]),
            'geometrie_rd_y': int(geom[1]),
            'geometrie_wgs_lat': (
                '{:.7f}'.format(geom_wgs[1])).replace('.', ','),

            'geometrie_wgs_lon': (
                '{:.7f}'.format(geom_wgs[0])).replace('.', ',')

        }
        item.update(res)
    def convertGeojsonFeatureToGEOS(self, feature):

        if "properties" in feature and "srid" in feature["properties"]:
            srid = feature["properties"]["srid"]
        else:
            srid = 4326

        try:
            geos = GEOSGeometry(json.dumps(feature["geometry"]), srid=srid)
        except:
            return None
        
        if geos.srid != self.srid_db: 
            ct = CoordTransform(SpatialReference(geos.srid), SpatialReference(self.srid_db))
            geos.transform(ct)

        return geos
    def handle_wkt(self, wkt, name):
        g1 = GEOSGeometry(wkt)
        srid = g1.srid
        if not srid:
            raise Exception("Unknown SRID. Try ewkt format; `SRID=4326;POLYGON((.....))`")
        if g1 and isinstance(g1, geos.Polygon):
            g1 = geos.MultiPolygon(g1)
            g1.srid = srid

        if not name:
            raise Exception("No --name provided!")

        g1.transform(settings.GEOMETRY_DB_SRID)
        region = StudyRegion.objects.create(geometry=g1, name=name, active=True)
        region.save()

        print "Study region created: %s, primary key = %s" % (region.name, region.pk)
Exemple #27
0
    def test06_f_expressions(self):
        "Testing F() expressions on GeometryFields."
        # Constructing a dummy parcel border and getting the City instance for
        # assigning the FK.
        b1 = GEOSGeometry(
            'POLYGON((-97.501205 33.052520,-97.501205 33.052576,'
            '-97.501150 33.052576,-97.501150 33.052520,-97.501205 33.052520))',
            srid=4326
        )
        pcity = City.objects.get(name='Aurora')

        # First parcel has incorrect center point that is equal to the City;
        # it also has a second border that is different from the first as a
        # 100ft buffer around the City.
        c1 = pcity.location.point
        c2 = c1.transform(2276, clone=True)
        b2 = c2.buffer(100)
        Parcel.objects.create(name='P1', city=pcity, center1=c1, center2=c2, border1=b1, border2=b2)

        # Now creating a second Parcel where the borders are the same, just
        # in different coordinate systems.  The center points are also the
        # same (but in different coordinate systems), and this time they
        # actually correspond to the centroid of the border.
        c1 = b1.centroid
        c2 = c1.transform(2276, clone=True)
        b2 = b1 if connection.features.supports_transform else b1.transform(2276, clone=True)
        Parcel.objects.create(name='P2', city=pcity, center1=c1, center2=c2, border1=b1, border2=b2)

        # Should return the second Parcel, which has the center within the
        # border.
        qs = Parcel.objects.filter(center1__within=F('border1'))
        self.assertEqual(1, len(qs))
        self.assertEqual('P2', qs[0].name)

        # This time center2 is in a different coordinate system and needs to be
        # wrapped in transformation SQL.
        qs = Parcel.objects.filter(center2__within=F('border1'))
        if connection.features.supports_transform:
            self.assertEqual('P2', qs.get().name)
        else:
            msg = "This backend doesn't support the Transform function."
            with self.assertRaisesMessage(NotImplementedError, msg):
                list(qs)

        # Should return the first Parcel, which has the center point equal
        # to the point in the City ForeignKey.
        qs = Parcel.objects.filter(center1=F('city__location__point'))
        self.assertEqual(1, len(qs))
        self.assertEqual('P1', qs[0].name)

        # This time the city column should be wrapped in transformation SQL.
        qs = Parcel.objects.filter(border2__contains=F('city__location__point'))
        if connection.features.supports_transform:
            self.assertEqual('P1', qs.get().name)
        else:
            msg = "This backend doesn't support the Transform function."
            with self.assertRaisesMessage(NotImplementedError, msg):
                list(qs)
Exemple #28
0
    def render(self, name, value, attrs=None):
        # Update the template parameters with any attributes passed in.
        if attrs: self.params.update(attrs)

        # Defaulting the WKT value to a blank string -- this
        # will be tested in the JavaScript and the appropriate
        # interfaace will be constructed.
        self.params['wkt'] = ''

        # If a string reaches here (via a validation error on another
        # field) then just reconstruct the Geometry.
        if isinstance(value, basestring):
            try:
                value = GEOSGeometry(value)
            except (GEOSException, ValueError):
                value = None

        if value and value.geom_type.upper() != self.geom_type:
            value = None

        # Constructing the dictionary of the map options.
        self.params['map_options'] = self.map_options()

        # Constructing the JavaScript module name using the ID of
        # the GeometryField (passed in via the `attrs` keyword).
        self.params['module'] = 'geodjango_%s' % self.params['field_name']

        if value:
            # Transforming the geometry to the projection used on the
            # OpenLayers map.
            srid = self.params['srid']
            if value.srid != srid: 
                try:
                    value.transform(srid)
                    wkt = value.wkt
                except OGRException:
                    wkt = ''
            else:
                wkt = value.wkt
               
            # Setting the parameter WKT with that of the transformed
            # geometry.
            self.params['wkt'] = wkt

        return render_to_string(self.template, self.params)
Exemple #29
0
def _buffer(geojson, width=0, unit='ft'):
    geojson = JSONSerializer().serialize(geojson)
    geom = GEOSGeometry(geojson, srid=4326)

    try:
        width = float(width)
    except:
        width = 0

    if width > 0:
        if unit == 'ft':
            width = width/3.28084

        geom.transform(settings.ANALYSIS_COORDINATE_SYSTEM_SRID)
        geom = geom.buffer(width)
        geom.transform(4326)

    return geom
Exemple #30
0
def _buffer(geojson, width=0, unit='ft'):
    geojson = JSONSerializer().serialize(geojson)
    geom = GEOSGeometry(geojson, srid=4326)

    try:
        width = float(width)
    except:
        width = 0

    if width > 0:
        if unit == 'ft':
            width = width/3.28084

        geom.transform(3857)
        geom = geom.buffer(width)
        geom.transform(4326)

    return geom
Exemple #31
0
def resourcebase_post_save(instance, *args, **kwargs):
    """
    Used to fill any additional fields after the save.
    Has to be called by the children
    """
    if not instance.id:
        return

    ResourceBase.objects.filter(id=instance.id).update(
        thumbnail_url=instance.get_thumbnail_url(),
        detail_url=instance.get_absolute_url(),
        csw_insert_date=datetime.datetime.now())
    instance.set_missing_info()

    # we need to remove stale links
    for link in instance.link_set.all():
        if link.name == "External Document":
            if link.resource.doc_url != link.url:
                link.delete()
        else:
            if urlsplit(settings.SITEURL).hostname not in link.url:
                link.delete()

    try:
        if instance.regions and instance.regions.all():
            """
            try:
                queryset = instance.regions.all().order_by('name')
                for region in queryset:
                    print ("%s : %s" % (region.name, region.geographic_bounding_box))
            except:
                tb = traceback.format_exc()
            else:
                tb = None
            finally:
                if tb:
                    logger.debug(tb)
            """
            pass
        else:
            srid1, wkt1 = instance.geographic_bounding_box.split(";")
            srid1 = re.findall(r'\d+', srid1)

            poly1 = GEOSGeometry(wkt1, srid=int(srid1[0]))
            poly1.transform(4326)

            queryset = Region.objects.all().order_by('name')
            global_regions = []
            regions_to_add = []
            for region in queryset:
                try:
                    srid2, wkt2 = region.geographic_bounding_box.split(";")
                    srid2 = re.findall(r'\d+', srid2)

                    poly2 = GEOSGeometry(wkt2, srid=int(srid2[0]))
                    poly2.transform(4326)

                    if poly2.intersection(poly1):
                        regions_to_add.append(region)
                    if region.level == 0 and region.parent is None:
                        global_regions.append(region)
                except:
                    tb = traceback.format_exc()
                    if tb:
                        logger.debug(tb)
            if regions_to_add or global_regions:
                if regions_to_add and len(regions_to_add) > 0 and len(
                        regions_to_add) <= 30:
                    instance.regions.add(*regions_to_add)
                else:
                    instance.regions.add(*global_regions)
    except:
        tb = traceback.format_exc()
        if tb:
            logger.debug(tb)

    # set default License if no specified
    if instance.license is None:
        no_license = License.objects.filter(name="Not Specified")

        if no_license and len(no_license) > 0:
            instance.license = no_license[0]
            instance.save()
Exemple #32
0
def addRunlet(run, old, new):

    if ((old.lat == new.lat) and (old.lon == new.lon)
            and (run['runlets'] == 0)):
        return

    path_id = getNearestPath(old)

    if (path_id == None):
        path_id = -1

    old_lat = old.lat
    old_lon = old.lon
    t0 = old.t

    new_lat = new.lat
    new_lon = new.lon
    tn = new.t

    if (old.dirTag != new.dirTag):
        old_lat = new_lat
        old_lon = new_lon
        t0 = tn

    sql = "insert into runlet (run_id,lat0,lon0,loc0,t0,latn,lonn,locn,tn,path_id,distance) values (" + str(
        run['id']) + "," + str(old_lat) + "," + str(
            old_lon) + ", ST_SetSRID(ST_MakePoint(" + str(old_lon) + "," + str(
                old_lat
            ) + " )," + srid + ")," + str(t0) + "," + str(new_lat) + "," + str(
                new_lon) + ",ST_SetSRID(ST_MakePoint(" + str(
                    new_lon) + "," + str(new_lat) + " )," + srid + ") ," + str(
                        tn) + "," + str(path_id) + ",0) returning id"

    cur = old.db.cursor()
    cur.execute(sql)
    old.db.commit()
    id = cur.fetchone()[0]

    GEOSGeom0 = GEOSGeometry("POINT(" + str(old.lon) + " " + str(old.lat) +
                             ")")
    GEOSGeom0.srid = 4326
    GEOSGeomn = GEOSGeometry("POINT(" + str(new.lon) + " " + str(new.lat) +
                             ")")
    GEOSGeomn.srid = 4326
    GEOSGeom0.transform(ct)
    GEOSGeomn.transform(ct)
    dist = GEOSGeom0.distance(GEOSGeomn)
    sql = "update runlet set distance=%s where id=%s"
    cur.execute(sql, (
        dist,
        id,
    ))
    old.db.commit()

    freq = int((t - run['startTime']) / (run['runlets'] + 1))

    sql = "update run set distance =  distance + %s, runlets = runlets + 1, freq = %s where id = " + str(
        run['id'])
    cur.execute(sql, (
        dist,
        freq,
    ))
    old.db.commit()
    """
Exemple #33
0
    def test06_f_expressions(self):
        "Testing F() expressions on GeometryFields."
        # Constructing a dummy parcel border and getting the City instance for
        # assigning the FK.
        b1 = GEOSGeometry(
            'POLYGON((-97.501205 33.052520,-97.501205 33.052576,'
            '-97.501150 33.052576,-97.501150 33.052520,-97.501205 33.052520))',
            srid=4326)
        pcity = City.objects.get(name='Aurora')

        # First parcel has incorrect center point that is equal to the City;
        # it also has a second border that is different from the first as a
        # 100ft buffer around the City.
        c1 = pcity.location.point
        c2 = c1.transform(2276, clone=True)
        b2 = c2.buffer(100)
        Parcel.objects.create(name='P1',
                              city=pcity,
                              center1=c1,
                              center2=c2,
                              border1=b1,
                              border2=b2)

        # Now creating a second Parcel where the borders are the same, just
        # in different coordinate systems.  The center points are also the
        # same (but in different coordinate systems), and this time they
        # actually correspond to the centroid of the border.
        c1 = b1.centroid
        c2 = c1.transform(2276, clone=True)
        b2 = b1 if connection.features.supports_transform else b1.transform(
            2276, clone=True)
        Parcel.objects.create(name='P2',
                              city=pcity,
                              center1=c1,
                              center2=c2,
                              border1=b1,
                              border2=b2)

        # Should return the second Parcel, which has the center within the
        # border.
        qs = Parcel.objects.filter(center1__within=F('border1'))
        self.assertEqual(1, len(qs))
        self.assertEqual('P2', qs[0].name)

        # This time center2 is in a different coordinate system and needs to be
        # wrapped in transformation SQL.
        qs = Parcel.objects.filter(center2__within=F('border1'))
        if connection.features.supports_transform:
            self.assertEqual('P2', qs.get().name)
        else:
            msg = "This backend doesn't support the Transform function."
            with self.assertRaisesMessage(NotSupportedError, msg):
                list(qs)

        # Should return the first Parcel, which has the center point equal
        # to the point in the City ForeignKey.
        qs = Parcel.objects.filter(center1=F('city__location__point'))
        self.assertEqual(1, len(qs))
        self.assertEqual('P1', qs[0].name)

        # This time the city column should be wrapped in transformation SQL.
        qs = Parcel.objects.filter(
            border2__contains=F('city__location__point'))
        if connection.features.supports_transform:
            self.assertEqual('P1', qs.get().name)
        else:
            msg = "This backend doesn't support the Transform function."
            with self.assertRaisesMessage(NotSupportedError, msg):
                list(qs)
Exemple #34
0
 def simplified_length(self):
     geometry = GEOSGeometry(self.simplified_track)
     geometry.transform(3857)
     return geometry.length
Exemple #35
0
    def test_ten_thousand_sq_km_aoi(self):
        aoi = GEOSGeometry(json.dumps({
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -115.01586914062499,
                        43.866218006556394
                    ],
                    [
                        -113.719482421875,
                        43.866218006556394
                    ],
                    [
                        -113.719482421875,
                        44.89479576469787
                    ],
                    [
                        -115.01586914062499,
                        44.89479576469787
                    ],
                    [
                        -115.01586914062499,
                        43.866218006556394
                    ]
                ]
            ]
        }), srid=4326)

        reprojected_aoi = aoi.transform(5070, clone=True)

        abutting_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -115.23559570312499,
                        44.380802793578475
                    ],
                    [
                        -115.00488281250001,
                        44.380802793578475
                    ],
                    [
                        -115.00488281250001,
                        44.52001001133986
                    ],
                    [
                        -115.23559570312499,
                        44.52001001133986
                    ],
                    [
                        -115.23559570312499,
                        44.380802793578475
                    ]
                ]
            ]
        }

        intersecting_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -115.17791748046875,
                        43.775060351224695
                    ],
                    [
                        -114.949951171875,
                        43.775060351224695
                    ],
                    [
                        -114.949951171875,
                        44.09350315285847
                    ],
                    [
                        -115.17791748046875,
                        44.09350315285847
                    ],
                    [
                        -115.17791748046875,
                        43.775060351224695
                    ]
                ]
            ]
        }

        contained_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -114.43359375,
                        44.262904233655384
                    ],
                    [
                        -114.06829833984375,
                        44.262904233655384
                    ],
                    [
                        -114.06829833984375,
                        44.61393394730626
                    ],
                    [
                        -114.43359375,
                        44.61393394730626
                    ],
                    [
                        -114.43359375,
                        44.262904233655384
                    ]
                ]
            ]
        }

        self.assertFalse(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                        abutting_catchment))
        self.assertTrue(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                       intersecting_catchment))
        self.assertTrue(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                       contained_catchment))
Exemple #36
0
class DistanceTest(TestCase):
    fixtures = ['initial']

    def setUp(self):
        # A point we are testing distances with -- using a WGS84
        # coordinate that'll be implicitly transformed to that to
        # the coordinate system of the field, EPSG:32140 (Texas South Central
        # w/units in meters)
        self.stx_pnt = GEOSGeometry('POINT (-95.370401017314293 29.704867409475465)', 4326)
        # Another one for Australia
        self.au_pnt = GEOSGeometry('POINT (150.791 -34.4919)', 4326)

    def get_names(self, qs):
        cities = [c.name for c in qs]
        cities.sort()
        return cities

    def test_init(self):
        """
        Test initialization of distance models.
        """
        self.assertEqual(9, SouthTexasCity.objects.count())
        self.assertEqual(9, SouthTexasCityFt.objects.count())
        self.assertEqual(11, AustraliaCity.objects.count())
        self.assertEqual(4, SouthTexasZipcode.objects.count())
        self.assertEqual(4, CensusZipcode.objects.count())
        self.assertEqual(1, Interstate.objects.count())
        self.assertEqual(1, SouthTexasInterstate.objects.count())

    @skipUnlessDBFeature("supports_dwithin_lookup")
    def test_dwithin(self):
        """
        Test the `dwithin` lookup type.
        """
        # Distances -- all should be equal (except for the
        # degree/meter pair in au_cities, that's somewhat
        # approximate).
        tx_dists = [(7000, 22965.83), D(km=7), D(mi=4.349)]
        au_dists = [(0.5, 32000), D(km=32), D(mi=19.884)]

        # Expected cities for Australia and Texas.
        tx_cities = ['Downtown Houston', 'Southside Place']
        au_cities = ['Mittagong', 'Shellharbour', 'Thirroul', 'Wollongong']

        # Performing distance queries on two projected coordinate systems one
        # with units in meters and the other in units of U.S. survey feet.
        for dist in tx_dists:
            if isinstance(dist, tuple):
                dist1, dist2 = dist
            else:
                dist1 = dist2 = dist
            qs1 = SouthTexasCity.objects.filter(point__dwithin=(self.stx_pnt, dist1))
            qs2 = SouthTexasCityFt.objects.filter(point__dwithin=(self.stx_pnt, dist2))
            for qs in qs1, qs2:
                with self.subTest(dist=dist, qs=qs):
                    self.assertEqual(tx_cities, self.get_names(qs))

        # With a complex geometry expression
        self.assertFalse(SouthTexasCity.objects.exclude(point__dwithin=(Union('point', 'point'), 0)))

        # Now performing the `dwithin` queries on a geodetic coordinate system.
        for dist in au_dists:
            with self.subTest(dist=dist):
                type_error = isinstance(dist, D) and not oracle
                if isinstance(dist, tuple):
                    if oracle or spatialite:
                        # Result in meters
                        dist = dist[1]
                    else:
                        # Result in units of the field
                        dist = dist[0]

                # Creating the query set.
                qs = AustraliaCity.objects.order_by('name')
                if type_error:
                    # A ValueError should be raised on PostGIS when trying to
                    # pass Distance objects into a DWithin query using a
                    # geodetic field.
                    with self.assertRaises(ValueError):
                        AustraliaCity.objects.filter(point__dwithin=(self.au_pnt, dist)).count()
                else:
                    self.assertEqual(au_cities, self.get_names(qs.filter(point__dwithin=(self.au_pnt, dist))))

    @skipUnlessDBFeature("supports_distances_lookups")
    def test_distance_lookups(self):
        """
        Test the `distance_lt`, `distance_gt`, `distance_lte`, and `distance_gte` lookup types.
        """
        # Retrieving the cities within a 20km 'donut' w/a 7km radius 'hole'
        # (thus, Houston and Southside place will be excluded as tested in
        # the `test02_dwithin` above).
        for model in [SouthTexasCity, SouthTexasCityFt]:
            stx_pnt = self.stx_pnt.transform(model._meta.get_field('point').srid, clone=True)
            qs = model.objects.filter(point__distance_gte=(stx_pnt, D(km=7))).filter(
                point__distance_lte=(stx_pnt, D(km=20)),
            )
            cities = self.get_names(qs)
            self.assertEqual(cities, ['Bellaire', 'Pearland', 'West University Place'])

        # Doing a distance query using Polygons instead of a Point.
        z = SouthTexasZipcode.objects.get(name='77005')
        qs = SouthTexasZipcode.objects.exclude(name='77005').filter(poly__distance_lte=(z.poly, D(m=275)))
        self.assertEqual(['77025', '77401'], self.get_names(qs))
        # If we add a little more distance 77002 should be included.
        qs = SouthTexasZipcode.objects.exclude(name='77005').filter(poly__distance_lte=(z.poly, D(m=300)))
        self.assertEqual(['77002', '77025', '77401'], self.get_names(qs))

    @skipUnlessDBFeature("supports_distances_lookups", "supports_distance_geodetic")
    def test_geodetic_distance_lookups(self):
        """
        Test distance lookups on geodetic coordinate systems.
        """
        # Line is from Canberra to Sydney.  Query is for all other cities within
        # a 100km of that line (which should exclude only Hobart & Adelaide).
        line = GEOSGeometry('LINESTRING(144.9630 -37.8143,151.2607 -33.8870)', 4326)
        dist_qs = AustraliaCity.objects.filter(point__distance_lte=(line, D(km=100)))
        expected_cities = [
            'Batemans Bay', 'Canberra', 'Hillsdale',
            'Melbourne', 'Mittagong', 'Shellharbour',
            'Sydney', 'Thirroul', 'Wollongong',
        ]
        if spatialite:
            # SpatiaLite is less accurate and returns 102.8km for Batemans Bay.
            expected_cities.pop(0)
        self.assertEqual(expected_cities, self.get_names(dist_qs))

        msg = "2, 3, or 4-element tuple required for 'distance_lte' lookup."
        with self.assertRaisesMessage(ValueError, msg):  # Too many params.
            len(AustraliaCity.objects.filter(point__distance_lte=('POINT(5 23)', D(km=100), 'spheroid', '4', None)))

        with self.assertRaisesMessage(ValueError, msg):  # Too few params.
            len(AustraliaCity.objects.filter(point__distance_lte=('POINT(5 23)',)))

        msg = "For 4-element tuples the last argument must be the 'spheroid' directive."
        with self.assertRaisesMessage(ValueError, msg):
            len(AustraliaCity.objects.filter(point__distance_lte=('POINT(5 23)', D(km=100), 'spheroid', '4')))

        # Getting all cities w/in 550 miles of Hobart.
        hobart = AustraliaCity.objects.get(name='Hobart')
        qs = AustraliaCity.objects.exclude(name='Hobart').filter(point__distance_lte=(hobart.point, D(mi=550)))
        cities = self.get_names(qs)
        self.assertEqual(cities, ['Batemans Bay', 'Canberra', 'Melbourne'])

        # Cities that are either really close or really far from Wollongong --
        # and using different units of distance.
        wollongong = AustraliaCity.objects.get(name='Wollongong')
        d1, d2 = D(yd=19500), D(nm=400)  # Yards (~17km) & Nautical miles.

        # Normal geodetic distance lookup (uses `distance_sphere` on PostGIS.
        gq1 = Q(point__distance_lte=(wollongong.point, d1))
        gq2 = Q(point__distance_gte=(wollongong.point, d2))
        qs1 = AustraliaCity.objects.exclude(name='Wollongong').filter(gq1 | gq2)

        # Geodetic distance lookup but telling GeoDjango to use `distance_spheroid`
        # instead (we should get the same results b/c accuracy variance won't matter
        # in this test case).
        querysets = [qs1]
        if connection.features.has_DistanceSpheroid_function:
            gq3 = Q(point__distance_lte=(wollongong.point, d1, 'spheroid'))
            gq4 = Q(point__distance_gte=(wollongong.point, d2, 'spheroid'))
            qs2 = AustraliaCity.objects.exclude(name='Wollongong').filter(gq3 | gq4)
            querysets.append(qs2)

        for qs in querysets:
            cities = self.get_names(qs)
            self.assertEqual(cities, ['Adelaide', 'Hobart', 'Shellharbour', 'Thirroul'])

    @skipUnlessDBFeature("supports_distances_lookups")
    def test_distance_lookups_with_expression_rhs(self):
        stx_pnt = self.stx_pnt.transform(SouthTexasCity._meta.get_field('point').srid, clone=True)
        qs = SouthTexasCity.objects.filter(
            point__distance_lte=(stx_pnt, F('radius')),
        ).order_by('name')
        self.assertEqual(
            self.get_names(qs),
            ['Bellaire', 'Downtown Houston', 'Southside Place', 'West University Place']
        )

        # With a combined expression
        qs = SouthTexasCity.objects.filter(
            point__distance_lte=(stx_pnt, F('radius') * 2),
        ).order_by('name')
        self.assertEqual(len(qs), 5)
        self.assertIn('Pearland', self.get_names(qs))

        # With spheroid param
        if connection.features.supports_distance_geodetic:
            hobart = AustraliaCity.objects.get(name='Hobart')
            qs = AustraliaCity.objects.filter(
                point__distance_lte=(hobart.point, F('radius') * 70, 'spheroid'),
            ).order_by('name')
            self.assertEqual(self.get_names(qs), ['Canberra', 'Hobart', 'Melbourne'])

        # With a complex geometry expression
        self.assertFalse(SouthTexasCity.objects.filter(point__distance_gt=(Union('point', 'point'), 0)))
        self.assertEqual(
            SouthTexasCity.objects.filter(point__distance_lte=(Union('point', 'point'), 0)).count(),
            SouthTexasCity.objects.count(),
        )

    @unittest.skipUnless(mysql, 'This is a MySQL-specific test')
    def test_mysql_geodetic_distance_error(self):
        msg = 'Only numeric values of degree units are allowed on geodetic distance queries.'
        with self.assertRaisesMessage(ValueError, msg):
            AustraliaCity.objects.filter(point__distance_lte=(Point(0, 0), D(m=100))).exists()

    @skipUnlessDBFeature('supports_dwithin_lookup')
    def test_dwithin_subquery(self):
        """dwithin lookup in a subquery using OuterRef as a parameter."""
        qs = CensusZipcode.objects.annotate(
            annotated_value=Exists(SouthTexasCity.objects.filter(
                point__dwithin=(OuterRef('poly'), D(m=10)),
            ))
        ).filter(annotated_value=True)
        self.assertEqual(self.get_names(qs), ['77002', '77025', '77401'])

    @skipUnlessDBFeature('supports_dwithin_lookup', 'supports_dwithin_distance_expr')
    def test_dwithin_with_expression_rhs(self):
        # LineString of Wollongong and Adelaide coords.
        ls = LineString(((150.902, -34.4245), (138.6, -34.9258)), srid=4326)
        qs = AustraliaCity.objects.filter(
            point__dwithin=(ls, F('allowed_distance')),
        ).order_by('name')
        self.assertEqual(
            self.get_names(qs),
            ['Adelaide', 'Mittagong', 'Shellharbour', 'Thirroul', 'Wollongong'],
        )

    @skipIfDBFeature('supports_dwithin_distance_expr')
    def test_dwithin_with_expression_rhs_not_supported(self):
        ls = LineString(((150.902, -34.4245), (138.6, -34.9258)), srid=4326)
        msg = (
            'This backend does not support expressions for specifying '
            'distance in the dwithin lookup.'
        )
        with self.assertRaisesMessage(NotSupportedError, msg):
            list(AustraliaCity.objects.filter(
                point__dwithin=(ls, F('allowed_distance')),
            ))
Exemple #37
0
 def transform3912(x, y):
     si_point = GEOSGeometry('SRID=3912;POINT (%s %s)' % (x, y))
     si_point.transform(_transform_3912)
     return si_point.x, si_point.y
Exemple #38
0
 def transform3787(x, y):
     si_point = GEOSGeometry('SRID=3787;POINT (%s %s)' % (x, y))
     si_point.transform(_transform_3787)
     return si_point.x, si_point.y
Exemple #39
0
    def test_dwithin_gis_lookup_ouptut_with_rasters(self):
        """
        Check the logical functionality of the dwithin lookup for different
        input parameters.
        """
        # Create test raster and geom.
        rast = GDALRaster(json.loads(JSON_RASTER))
        stx_pnt = GEOSGeometry(
            'POINT (-95.370401017314293 29.704867409475465)', 4326)
        stx_pnt.transform(3086)

        # Filter raster with different lookup raster formats.
        qs = RasterModel.objects.filter(rastprojected__dwithin=(rast, D(km=1)))
        self.assertEqual(qs.count(), 1)

        qs = RasterModel.objects.filter(
            rastprojected__dwithin=(json.loads(JSON_RASTER), D(km=1)))
        self.assertEqual(qs.count(), 1)

        qs = RasterModel.objects.filter(rastprojected__dwithin=(JSON_RASTER,
                                                                D(km=1)))
        self.assertEqual(qs.count(), 1)

        # Filter in an unprojected coordinate system.
        qs = RasterModel.objects.filter(rast__dwithin=(rast, 40))
        self.assertEqual(qs.count(), 1)

        # Filter with band index transform.
        qs = RasterModel.objects.filter(rast__1__dwithin=(rast, 1, 40))
        self.assertEqual(qs.count(), 1)
        qs = RasterModel.objects.filter(rast__1__dwithin=(rast, 40))
        self.assertEqual(qs.count(), 1)
        qs = RasterModel.objects.filter(rast__dwithin=(rast, 1, 40))
        self.assertEqual(qs.count(), 1)

        # Filter raster by geom.
        qs = RasterModel.objects.filter(rast__dwithin=(stx_pnt, 500))
        self.assertEqual(qs.count(), 1)

        qs = RasterModel.objects.filter(rastprojected__dwithin=(stx_pnt,
                                                                D(km=10000)))
        self.assertEqual(qs.count(), 1)

        qs = RasterModel.objects.filter(rast__dwithin=(stx_pnt, 5))
        self.assertEqual(qs.count(), 0)

        qs = RasterModel.objects.filter(rastprojected__dwithin=(stx_pnt,
                                                                D(km=100)))
        self.assertEqual(qs.count(), 0)

        # Filter geom by raster.
        qs = RasterModel.objects.filter(geom__dwithin=(rast, 500))
        self.assertEqual(qs.count(), 1)

        # Filter through related model.
        qs = RasterRelatedModel.objects.filter(
            rastermodel__rast__dwithin=(rast, 40))
        self.assertEqual(qs.count(), 1)

        # Filter through related model with band index transform
        qs = RasterRelatedModel.objects.filter(
            rastermodel__rast__1__dwithin=(rast, 40))
        self.assertEqual(qs.count(), 1)

        # Filter through conditional statements.
        qs = RasterModel.objects.filter(
            Q(rast__dwithin=(rast, 40))
            & Q(rastprojected__dwithin=(stx_pnt, D(km=10000))))
        self.assertEqual(qs.count(), 1)

        # Filter through different lookup.
        qs = RasterModel.objects.filter(rastprojected__bbcontains=rast)
        self.assertEqual(qs.count(), 1)
Exemple #40
0
    def test_all_gis_lookups_with_rasters(self):
        """
        Evaluate all possible lookups for all input combinations (i.e.
        raster-raster, raster-geom, geom-raster) and for projected and
        unprojected coordinate systems. This test just checks that the lookup
        can be called, but doesn't check if the result makes logical sense.
        """
        from django.contrib.gis.db.backends.postgis.operations import PostGISOperations

        # Create test raster and geom.
        rast = GDALRaster(json.loads(JSON_RASTER))
        stx_pnt = GEOSGeometry(
            'POINT (-95.370401017314293 29.704867409475465)', 4326)
        stx_pnt.transform(3086)

        lookups = [(name, lookup)
                   for name, lookup in BaseSpatialField.get_lookups().items()
                   if issubclass(lookup, GISLookup)]
        self.assertNotEqual(lookups, [], 'No lookups found')
        # Loop through all the GIS lookups.
        for name, lookup in lookups:
            # Construct lookup filter strings.
            combo_keys = [
                field + name for field in [
                    'rast__',
                    'rast__',
                    'rastprojected__0__',
                    'rast__',
                    'rastprojected__',
                    'geom__',
                    'rast__',
                ]
            ]
            if issubclass(lookup, DistanceLookupBase):
                # Set lookup values for distance lookups.
                combo_values = [
                    (rast, 50, 'spheroid'),
                    (rast, 0, 50, 'spheroid'),
                    (rast, 0, D(km=1)),
                    (stx_pnt, 0, 500),
                    (stx_pnt, D(km=1000)),
                    (rast, 500),
                    (json.loads(JSON_RASTER), 500),
                ]
            elif name == 'relate':
                # Set lookup values for the relate lookup.
                combo_values = [
                    (rast, 'T*T***FF*'),
                    (rast, 0, 'T*T***FF*'),
                    (rast, 0, 'T*T***FF*'),
                    (stx_pnt, 0, 'T*T***FF*'),
                    (stx_pnt, 'T*T***FF*'),
                    (rast, 'T*T***FF*'),
                    (json.loads(JSON_RASTER), 'T*T***FF*'),
                ]
            elif name == 'isvalid':
                # The isvalid lookup doesn't make sense for rasters.
                continue
            elif PostGISOperations.gis_operators[name].func:
                # Set lookup values for all function based operators.
                combo_values = [
                    rast, (rast, 0), (rast, 0), (stx_pnt, 0), stx_pnt, rast,
                    json.loads(JSON_RASTER)
                ]
            else:
                # Override band lookup for these, as it's not supported.
                combo_keys[2] = 'rastprojected__' + name
                # Set lookup values for all other operators.
                combo_values = [
                    rast, None, rast, stx_pnt, stx_pnt, rast,
                    json.loads(JSON_RASTER)
                ]

            # Create query filter combinations.
            self.assertEqual(
                len(combo_keys),
                len(combo_values),
                'Number of lookup names and values should be the same',
            )
            combos = [x for x in zip(combo_keys, combo_values) if x[1]]
            self.assertEqual(
                [(n, x) for n, x in enumerate(combos) if x in combos[:n]],
                [],
                'There are repeated test lookups',
            )
            combos = [{k: v} for k, v in combos]

            for combo in combos:
                # Apply this query filter.
                qs = RasterModel.objects.filter(**combo)

                # Evaluate normal filter qs.
                self.assertIn(qs.count(), [0, 1])

            # Evaluate on conditional Q expressions.
            qs = RasterModel.objects.filter(Q(**combos[0]) & Q(**combos[1]))
            self.assertIn(qs.count(), [0, 1])
Exemple #41
0
    def save(self, tile, request):
        tile_edits = json.loads(request.POST.get('data'))['data']
        if request:
            address = {
                "geometry": {
                    "spatialReference": {
                        "wkid": 102100,
                        "latestWkid": 3857
                    }
                },
                "attributes": {
                    "EAS_BaseID": None,
                    "EAS_SubID": None,
                    "CNN": None,
                    "Address": None,
                    "Address_Number": None,
                    "Address_Number_Suffix": None,
                    "Street_Name": None,
                    "Street_Type": None,
                    "Unit_Number": None,
                    "Zipcode": None,
                    "Block_Lot": None,
                    "Longitude": None,
                    "Latitude": None,
                    "Location": None
                }
            }

            payload = {
                "adds":[],
                "updates":[],
                "deletes":'',
                "attachments":[],
                "rollbackOnFailure": False,
                "useGlobalIds": False,
                "f": "pjson",
                "token": "tTzVkJ7RPpZmqmlxc7xVBaORWK8vIKQenSkbmK13OnDfIHNKaNCIaH3i6Nz1AUbdnqkEsz8HuA-QqYrndP4yyqgov0NUgabK3lOO19erL-YYPtbIhEzahbSeQ0wPkJx1TH7RVL-gJ9m3iBsV9Affr0NczrLunSdj6rsa1Kg4QI8fTWpdgj0VCy7FaANWggjI6b7kDATtb43W9-hHxmndcjEU9S7lBzCfTty1b4GnAF3dmYhoh4ZBLC-XpsLetKEJ"
            }

            field_lookup = {
                "29862afe-4746-11e8-88b1-0242ac120006": "Block_Lot",
                "1a08f610-4746-11e8-b7cc-0242ac120006": "Street Name",
                "1a08fbd8-4746-11e8-b7cc-0242ac120006": "Address_Number",
                "1a08f3cc-4746-11e8-b7cc-0242ac120006": "Address_Number_Suffix",
                "1a08f80e-4746-11e8-b7cc-0242ac120006": "CNN",
            }

            geometry_node = '2ad20702-4746-11e8-a9a0-0242ac120006'


            result_node = models.Node.objects.get(pk='1a08f3cc-4746-11e8-b7cc-0242ac120006')
            external_reference = Tile.objects.filter(nodegroup=result_node.nodegroup).filter(resourceinstance=tile.resourceinstance_id)
            tiles = Tile.objects.filter(resourceinstance=tile.resourceinstance_id)

            has_geom = False

            for tile in tiles:
                for tile_node, tile_value in tile.data.iteritems():
                    # if models.Node.objects.get(pk=tile_node).datatype == 'geojson-feature-collection':
                    if tile_node == geometry_node:
                        geom = GEOSGeometry(json.dumps(tile_value['features'][0]['geometry']), srid=4326)
                        geom.transform(3857)
                        address['geometry']['x'] = geom.x
                        address['geometry']['y'] = geom.y
                        has_geom = True
                    if tile_node in field_lookup:
                        address["attributes"][field_lookup[tile_node]] = str(tile_value)

            for edit_node, edit_value in tile_edits.iteritems():
                if edit_node == geometry_node:
                    geom = GEOSGeometry(json.dumps(edit_value['features'][0]['geometry']), srid=4326)
                    geom.transform(3857)
                    address['geometry']['x'] = geom.x
                    address['geometry']['y'] = geom.y
                    has_geom = True
                if edit_node in field_lookup:
                    address["attributes"][field_lookup[edit_node]] = str(edit_value)

            if has_geom:
                if len(external_reference) != 0:
                    address["attributes"]["FID"] = int(external_reference[0].data["1a08f3cc-4746-11e8-b7cc-0242ac120006"])
                    payload["updates"].append(address)
                else:
                    payload["adds"].append(address)
                data = urllib.urlencode(payload).replace('None', 'null')
                url = self.config['external_address_url'] + '/applyEdits'
                req = urllib2.Request(url, data)
                f = urllib2.urlopen(req)
                response = f.read()
                response = json.loads(response)
                pp(payload)
                pp(response)
                if len(response['addResults']) > 0:
                    if response['addResults'][0]['success'] == True:
                        result_tile = models.TileModel()
                        result_tile.resourceinstance = models.ResourceInstance.objects.get(pk=tile.resourceinstance_id)
                        result_tile.data = {"1a08f3cc-4746-11e8-b7cc-0242ac120006": str(response['addResults'][0]['objectId'])}
                        result_tile.nodegroup = result_node.nodegroup
                        result_tile.save()
                f.close()

        return tile
Exemple #42
0
def layer_detail(request, layername, template='layers/layer_detail.html'):
    layer = _resolve_layer(request, layername, 'base.view_resourcebase',
                           _PERMISSION_MSG_VIEW)

    # assert False, str(layer_bbox)
    config = layer.attribute_config()

    # Add required parameters for GXP lazy-loading
    layer_bbox = layer.bbox
    bbox = [float(coord) for coord in list(layer_bbox[0:4])]
    if hasattr(layer, 'srid'):
        config['crs'] = {'type': 'name', 'properties': layer.srid}
    config["srs"] = getattr(settings, 'DEFAULT_MAP_CRS', 'EPSG:900913')
    config["bbox"] = bbox if config["srs"] != 'EPSG:900913' \
        else llbbox_to_mercator([float(coord) for coord in bbox])
    config["title"] = layer.title
    config["queryable"] = True

    if layer.storeType == "remoteStore":
        service = layer.service
        source_params = {
            "ptype": service.ptype,
            "remote": True,
            "url": service.base_url,
            "name": service.name
        }
        maplayer = GXPLayer(name=layer.alternate,
                            ows_url=layer.ows_url,
                            layer_params=json.dumps(config),
                            source_params=json.dumps(source_params))
    else:
        maplayer = GXPLayer(name=layer.alternate,
                            ows_url=layer.ows_url,
                            layer_params=json.dumps(config))

    # Update count for popularity ranking,
    # but do not includes admins or resource owners
    layer.view_count_up(request.user)

    # center/zoom don't matter; the viewer will center on the layer bounds
    map_obj = GXPMap(
        projection=getattr(settings, 'DEFAULT_MAP_CRS', 'EPSG:900913'))

    NON_WMS_BASE_LAYERS = [
        la for la in default_map_config(request)[1] if la.ows_url is None
    ]

    metadata = layer.link_set.metadata().filter(
        name__in=settings.DOWNLOAD_FORMATS_METADATA)

    granules = None
    all_granules = None
    filter = None
    if layer.is_mosaic:
        try:
            cat = gs_catalog
            cat._cache.clear()
            store = cat.get_store(layer.name)
            coverages = cat.mosaic_coverages(store)

            filter = None
            try:
                if request.GET["filter"]:
                    filter = request.GET["filter"]
            except BaseException:
                pass

            offset = 10 * (request.page - 1)
            granules = cat.mosaic_granules(
                coverages['coverages']['coverage'][0]['name'],
                store,
                limit=10,
                offset=offset,
                filter=filter)
            all_granules = cat.mosaic_granules(
                coverages['coverages']['coverage'][0]['name'],
                store,
                filter=filter)
        except BaseException:
            granules = {"features": []}
            all_granules = {"features": []}

    group = None
    if layer.group:
        try:
            group = GroupProfile.objects.get(slug=layer.group.name)
        except GroupProfile.DoesNotExist:
            group = None
    # a flag to be used for qgis server
    show_popup = False
    if 'show_popup' in request.GET and request.GET["show_popup"]:
        show_popup = True

    context_dict = {
        'resource': layer,
        'group': group,
        'perms_list': get_perms(request.user, layer.get_self_resource()),
        "permissions_json": _perms_info_json(layer),
        "documents": get_related_documents(layer),
        "metadata": metadata,
        "is_layer": True,
        "wps_enabled": settings.OGC_SERVER['default']['WPS_ENABLED'],
        "granules": granules,
        "all_granules": all_granules,
        "show_popup": show_popup,
        "filter": filter,
    }

    if 'access_token' in request.session:
        access_token = request.session['access_token']
    else:
        u = uuid.uuid1()
        access_token = u.hex

    context_dict["viewer"] = json.dumps(
        map_obj.viewer_json(request.user, access_token,
                            *(NON_WMS_BASE_LAYERS + [maplayer])))
    context_dict["preview"] = getattr(settings, 'LAYER_PREVIEW_LIBRARY',
                                      'leaflet')
    context_dict["crs"] = getattr(settings, 'DEFAULT_MAP_CRS', 'EPSG:900913')

    # provide bbox in EPSG:4326 for leaflet
    if context_dict["preview"] == 'leaflet':
        srid, wkt = layer.geographic_bounding_box.split(';')
        srid = re.findall(r'\d+', srid)
        geom = GEOSGeometry(wkt, srid=int(srid[0]))
        geom.transform(4326)
        context_dict["layer_bbox"] = ','.join([str(c) for c in geom.extent])

    if layer.storeType == 'dataStore':
        links = layer.link_set.download().filter(
            name__in=settings.DOWNLOAD_FORMATS_VECTOR)
    else:
        links = layer.link_set.download().filter(
            name__in=settings.DOWNLOAD_FORMATS_RASTER)
    links_view = [
        item for idx, item in enumerate(links)
        if item.url and 'wms' in item.url or 'gwc' in item.url
    ]
    links_download = [
        item for idx, item in enumerate(links)
        if item.url and 'wms' not in item.url and 'gwc' not in item.url
    ]
    for item in links_view:
        if item.url and access_token and 'access_token' not in item.url:
            params = {'access_token': access_token}
            item.url = Request('GET', item.url, params=params).prepare().url
    for item in links_download:
        if item.url and access_token and 'access_token' not in item.url:
            params = {'access_token': access_token}
            item.url = Request('GET', item.url, params=params).prepare().url

    if request.user.has_perm('view_resourcebase', layer.get_self_resource()):
        context_dict["links"] = links_view
    if request.user.has_perm('download_resourcebase',
                             layer.get_self_resource()):
        if layer.storeType == 'dataStore':
            links = layer.link_set.download().filter(
                name__in=settings.DOWNLOAD_FORMATS_VECTOR)
        else:
            links = layer.link_set.download().filter(
                name__in=settings.DOWNLOAD_FORMATS_RASTER)
        context_dict["links_download"] = links_download

    if settings.SOCIAL_ORIGINS:
        context_dict["social_links"] = build_social_links(request, layer)
    layers_names = layer.alternate
    try:
        if 'geonode' in layers_names:
            workspace, name = layers_names.split(':', 1)
        else:
            name = layers_names
    except:
        print "Can not identify workspace type and layername"

    context_dict["layer_name"] = json.dumps(layers_names)

    try:
        # get type of layer (raster or vector)
        if layer.storeType == 'coverageStore':
            context_dict["layer_type"] = "raster"
        elif layer.storeType == 'dataStore':
            context_dict["layer_type"] = "vector"

            location = "{location}{service}".format(
                **{
                    'location': settings.OGC_SERVER['default']['LOCATION'],
                    'service': 'wms',
                })
            # get schema for specific layer
            username = settings.OGC_SERVER['default']['USER']
            password = settings.OGC_SERVER['default']['PASSWORD']
            schema = get_schema(location,
                                name,
                                username=username,
                                password=password)

            # get the name of the column which holds the geometry
            if 'the_geom' in schema['properties']:
                schema['properties'].pop('the_geom', None)
            elif 'geom' in schema['properties']:
                schema['properties'].pop("geom", None)

            # filter the schema dict based on the values of layers_attributes
            layer_attributes_schema = []
            for key in schema['properties'].keys():
                layer_attributes_schema.append(key)

            filtered_attributes = layer_attributes_schema
            context_dict["schema"] = schema
            context_dict["filtered_attributes"] = filtered_attributes

    except:
        print "Possible error with OWSLib. Turning all available properties to string"

    # maps owned by user needed to fill the "add to existing map section" in template
    if request.user.is_authenticated():
        context_dict["maps"] = Map.objects.filter(owner=request.user)
    return TemplateResponse(request, template,
                            RequestContext(request, context_dict))
Exemple #43
0
    def test_huge_catchments_tiny_aoi(self):
        aoi = GEOSGeometry(json.dumps({
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -86.1189079284668,
                        30.712618489700507
                    ],
                    [
                        -86.11066818237303,
                        30.712618489700507
                    ],
                    [
                        -86.11066818237303,
                        30.719554693895116
                    ],
                    [
                        -86.1189079284668,
                        30.719554693895116
                    ],
                    [
                        -86.1189079284668,
                        30.712618489700507
                    ]
                ]
            ]
        }), srid=4326)

        reprojected_aoi = aoi.transform(5070, clone=True)

        abutting_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -86.11856460571288,
                        30.71940712027702
                    ],
                    [
                        -86.12113952636719,
                        30.88395860861961
                    ],
                    [
                        -86.38206481933594,
                        30.884547891921986
                    ],
                    [
                        -86.37931823730467,
                        30.71586528568626
                    ],
                    [
                        -86.11856460571288,
                        30.71940712027702
                    ]
                ]
            ]
        }

        intersecting_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -86.13006591796874,
                        30.59832078510471
                    ],
                    [
                        -85.9075927734375,
                        30.59832078510471
                    ],
                    [
                        -85.9075927734375,
                        30.714094319607913
                    ],
                    [
                        -86.13006591796874,
                        30.714094319607913
                    ],
                    [
                        -86.13006591796874,
                        30.59832078510471
                    ]
                ]
            ]
        }

        containing_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -86.22550964355469,
                        30.627277165616874
                    ],
                    [
                        -86.0394287109375,
                        30.627277165616874
                    ],
                    [
                        -86.0394287109375,
                        30.80967992229391
                    ],
                    [
                        -86.22550964355469,
                        30.80967992229391
                    ],
                    [
                        -86.22550964355469,
                        30.627277165616874
                    ]
                ]
            ]
        }

        self.assertFalse(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                        abutting_catchment))
        self.assertTrue(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                       intersecting_catchment))
        self.assertTrue(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                       containing_catchment))
Exemple #44
0
    def test_hundred_sq_km_aoi(self):
        aoi = GEOSGeometry(json.dumps({
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -94.64584350585938,
                        38.96154447940714
                    ],
                    [
                        -94.53460693359374,
                        38.96154447940714
                    ],
                    [
                        -94.53460693359374,
                        39.05225165582583
                    ],
                    [
                        -94.64584350585938,
                        39.05225165582583
                    ],
                    [
                        -94.64584350585938,
                        38.96154447940714
                    ]
                ]
            ]
        }), srid=4326)
        reprojected_aoi = aoi.transform(5070, clone=True)

        abutting_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -94.53563690185547,
                        39.03065255999985
                    ],
                    [
                        -94.49203491210938,
                        39.03065255999985
                    ],
                    [
                        -94.49203491210938,
                        39.07864158248181
                    ],
                    [
                        -94.53563690185547,
                        39.07864158248181
                    ],
                    [
                        -94.53563690185547,
                        39.03065255999985
                    ]
                ]
            ]
        }

        intersecting_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -94.55554962158203,
                        38.92870117926206
                    ],
                    [
                        -94.49581146240233,
                        38.92870117926206
                    ],
                    [
                        -94.49581146240233,
                        38.9858333874019
                    ],
                    [
                        -94.55554962158203,
                        38.9858333874019
                    ],
                    [
                        -94.55554962158203,
                        38.92870117926206
                    ]
                ]
            ]
        }

        contained_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -94.62284088134766,
                        38.997841307500714
                    ],
                    [
                        -94.58576202392578,
                        38.997841307500714
                    ],
                    [
                        -94.58576202392578,
                        39.031452644263084
                    ],
                    [
                        -94.62284088134766,
                        39.031452644263084
                    ],
                    [
                        -94.62284088134766,
                        38.997841307500714
                    ]
                ]
            ]
        }

        self.assertFalse(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                        abutting_catchment))
        self.assertTrue(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                       intersecting_catchment))
        self.assertTrue(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                       contained_catchment))
Exemple #45
0
    def test_huge_aoi_tiny_catchments(self):
        aoi = GEOSGeometry(json.dumps({
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -85.166015625,
                        39.470125122358176
                    ],
                    [
                        -82.44140625,
                        39.470125122358176
                    ],
                    [
                        -82.44140625,
                        42.94033923363181
                    ],
                    [
                        -85.166015625,
                        42.94033923363181
                    ],
                    [
                        -85.166015625,
                        39.470125122358176
                    ]
                ]
            ]
        }), srid=4326)

        reprojected_aoi = aoi.transform(5070, clone=True)

        abutting_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -85.440673828125,
                        42.68243539838623
                    ],
                    [
                        -85.15502929687499,
                        42.68243539838623
                    ],
                    [
                        -85.15502929687499,
                        42.79540065303723
                    ],
                    [
                        -85.440673828125,
                        42.79540065303723
                    ],
                    [
                        -85.440673828125,
                        42.68243539838623
                    ]
                ]
            ]
        }

        intersecting_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -82.63916015625,
                        41.94314874732696
                    ],
                    [
                        -82.265625,
                        41.94314874732696
                    ],
                    [
                        -82.265625,
                        42.06560675405716
                    ],
                    [
                        -82.63916015625,
                        42.06560675405716
                    ],
                    [
                        -82.63916015625,
                        41.94314874732696
                    ]
                ]
            ]
        }

        contained_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -83.671875,
                        39.65645604812829
                    ],
                    [
                        -83.34228515625,
                        39.65645604812829
                    ],
                    [
                        -83.34228515625,
                        39.9434364619742
                    ],
                    [
                        -83.671875,
                        39.9434364619742
                    ],
                    [
                        -83.671875,
                        39.65645604812829
                    ]
                ]
            ]
        }

        self.assertFalse(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                        abutting_catchment))
        self.assertTrue(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                       intersecting_catchment))
        self.assertTrue(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                       contained_catchment))
Exemple #46
0
def metadata_post_save(instance, *args, **kwargs):
    logger.debug("handling UUID In pre_save_dataset")
    if isinstance(instance, Dataset) and hasattr(
            settings,
            'LAYER_UUID_HANDLER') and settings.LAYER_UUID_HANDLER != '':
        logger.debug("using custom uuid handler In pre_save_dataset")
        from ..layers.utils import get_uuid_handler
        _uuid = get_uuid_handler()(instance).create_uuid()
        if _uuid != instance.uuid:
            instance.uuid = _uuid
            Dataset.objects.filter(id=instance.id).update(uuid=_uuid)

    # Fixup bbox
    if instance.bbox_polygon is None:
        instance.set_bbox_polygon((-180, -90, 180, 90), 'EPSG:4326')
    instance.set_bounds_from_bbox(instance.bbox_polygon, instance.srid
                                  or instance.bbox_polygon.srid)

    # Set a default user for accountstream to work correctly.
    if instance.owner is None:
        instance.owner = get_valid_user()

    if not instance.uuid:
        instance.uuid = str(uuid.uuid1())

    # set default License if no specified
    if instance.license is None:
        license = License.objects.filter(name="Not Specified")
        if license and len(license) > 0:
            instance.license = license[0]

    instance.thumbnail_url = instance.get_thumbnail_url()
    instance.detail_url = instance.get_absolute_url()
    instance.csw_insert_date = datetime.datetime.now(
        timezone.get_current_timezone())
    instance.set_missing_info()

    ResourceBase.objects.filter(id=instance.id).update(
        uuid=instance.uuid,
        srid=instance.srid,
        alternate=instance.alternate,
        bbox_polygon=instance.bbox_polygon,
        thumbnail_url=instance.get_thumbnail_url(),
        detail_url=instance.get_absolute_url(),
        csw_insert_date=datetime.datetime.now(timezone.get_current_timezone()))

    try:
        if not instance.regions or instance.regions.count() == 0:
            srid1, wkt1 = instance.geographic_bounding_box.split(";")
            srid1 = re.findall(r'\d+', srid1)

            poly1 = GEOSGeometry(wkt1, srid=int(srid1[0]))
            poly1.transform(4326)

            queryset = Region.objects.all().order_by('name')
            global_regions = []
            regions_to_add = []
            for region in queryset:
                try:
                    srid2, wkt2 = region.geographic_bounding_box.split(";")
                    srid2 = re.findall(r'\d+', srid2)

                    poly2 = GEOSGeometry(wkt2, srid=int(srid2[0]))
                    poly2.transform(4326)

                    if poly2.intersection(poly1):
                        regions_to_add.append(region)
                    if region.level == 0 and region.parent is None:
                        global_regions.append(region)
                except Exception:
                    tb = traceback.format_exc()
                    if tb:
                        logger.debug(tb)
            if regions_to_add or global_regions:
                if regions_to_add and len(regions_to_add) > 0 and len(
                        regions_to_add) <= 30:
                    instance.regions.add(*regions_to_add)
                else:
                    instance.regions.add(*global_regions)
    except Exception:
        tb = traceback.format_exc()
        if tb:
            logger.debug(tb)
    finally:
        # refresh catalogue metadata records
        from ..catalogue.models import catalogue_post_save
        catalogue_post_save(instance=instance, sender=instance.__class__)
Exemple #47
0
    def setUp(self):
        self.client = Client()

        # Create 3 users
        self.password = '******'
        self.user = User.objects.create_user('user1',
                                             '*****@*****.**',
                                             password=self.password)
        self.user2 = User.objects.create_user('user2',
                                              '*****@*****.**',
                                              password=self.password)

        self.group1 = Group.objects.create(name="Test Group 1")
        self.group1.save()
        self.user.groups.add(self.group1)
        self.user2.groups.add(self.group1)
        enable_sharing(self.group1)
        self.public = Group.objects.get(
            name=settings.SHARING_TO_PUBLIC_GROUPS[0])
        self.user.groups.add(self.public)

        # Create some necessary objects
        g1 = GEOSGeometry(
            'SRID=4326;POLYGON((-120.42 34.37, -119.64 34.32, -119.63 34.12, -120.44 34.15, -120.42 34.37))'
        )
        g1.transform(settings.GEOMETRY_DB_SRID)

        # Create 3 Mpas by different users
        mpa1 = TestMpa.objects.create(name='Test_MPA_1',
                                      designation='R',
                                      user=self.user,
                                      geometry_orig=g1)
        mpa1.save()
        mpa2 = TestMpa.objects.create(name='Test_MPA_2',
                                      designation='P',
                                      user=self.user,
                                      geometry_orig=g1)
        mpa2.save()
        mpa3 = TestMpa.objects.create(name='Test_MPA_3',
                                      designation='C',
                                      user=self.user,
                                      geometry_orig=g1)
        mpa3.save()
        self.mpa_ids = [mpa1.pk, mpa2.pk, mpa3.pk]
        self.mpa_uids = ['%s_%s' % (mpa1.model_uid(), x) for x in self.mpa_ids]

        # User1 adds mpa to an array
        array1 = TestArray.objects.create(name='Test_Array_1', user=self.user)
        array1.save()
        array2 = TestArray.objects.create(name='Test_Array_2', user=self.user)
        array2.save()
        mpa1.add_to_collection(array1)
        mpa2.add_to_collection(array2)

        array1.share_with(self.group1)
        array2.share_with(self.public)

        self.array_ids = [array1.pk, array2.pk]
        self.array_uids = [
            '%s_%s' % (array1.model_uid(), x) for x in self.array_ids
        ]
def get_pdf(areadata={}):

    session2 = session_asdc
    if debuglevel >= 1: print '{area_type} {area_code} {area_name}'.format(**areadata)

    # prepare post data for geoserver request to create map pdf
    mappdf_requestdata_str = Template(areadata['createjson']).substitute(**areadata)
    mappdf_requestdata = json.loads(mappdf_requestdata_str)
    areadata['multipdf_url_add'] = ''
    if (areadata['area_type'] == 'drawarea'):
        poly = GEOSGeometry(areadata['wkt_drawarea'], srid=4326)
        areadata['multipdf_url_add'] = '&flag=drawArea&filter={0}'.format(urllib.quote(poly.ewkt))
        poly.transform(3857)
        mappdf_requestdata['layers'][-1]['geoJson']['features'][0]['geometry']['coordinates'][1] = list(poly[0])
    elif (areadata['area_type'] in ['district', 'province']):
        areadata['multipdf_url_add'] = '&code={0}'.format(areadata[areadata['key_prefix']+'_code'])

    # transform projection from srid 4326 to 3857
    if debuglevel >= 2: print 'areadata[\'bbox\']', areadata['bbox']
    bboxlist = map(float, areadata['bbox'].split(","))
    if debuglevel >= 2: print 'bboxlist', bboxlist
    p = Point(bboxlist[0], bboxlist[1], srid=4326)
    p.transform(3857)
    p2 = Point(bboxlist[2], bboxlist[3], srid=4326)
    p2.transform(3857)

    mappdf_requestdata['pages'][0]['bbox'] = [p.x, p.y, p2.x, p2.y]
    if debuglevel >= 3: print 'mappdf_requestdata[\'pages\'][0][\'bbox\']', mappdf_requestdata['pages'][0]['bbox']
    mappdf_requestdata['pages'][0]['center'] = [(p.x+p2.x)/2, (p.y+p2.y)/2]
    width = bboxlist[2]-bboxlist[0]
    height = bboxlist[3]-bboxlist[1]
    # lengthNorthSouth = height * (polar_circumference/(2*math.pi))
    if debuglevel >= 3: print 'radian width', width
    if debuglevel >= 3: print 'radian height', height
    if debuglevel >= 3: print 'areadata[\'page_orientation\']', areadata['page_orientation']
    frame_width = frame[areadata['page_orientation']]['width']
    frame_height = frame[areadata['page_orientation']]['height']
    if debuglevel >= 3: print 'frame_width', frame_width
    if debuglevel >= 3: print 'frame_height', frame_height
    if debuglevel >= 3: print 'if ((frame_width < (width/(scale*radtopixelratio_x))) and (frame_height < (height/(scale*radtopixelratio_y)))))'
    for scale in scales:
        geomwidth_px = width/(scale*radtopixelratio_x)
        geomheight_px = height/(scale*radtopixelratio_y)
        if debuglevel >= 3: print 'if ((({0} > ({1}/({2}*{3}))) and (({4} > ({5}/({6}*{7})))) '.format(frame_width,width,scale,radtopixelratio_x,frame_height,height,scale,radtopixelratio_y)
        if debuglevel >= 3: print 'if (({0} > ({1})) and ({2} > ({3})))'.format(frame_width,geomwidth_px,frame_height,geomheight_px)
        # select scale where geometry fit inside frame
        if ((frame_width > geomwidth_px) and (frame_height > geomheight_px)):
            mappdf_requestdata['pages'][0]['scale'] = scale
            # mappdf_requestdata['pages'][0]['scale'] = 200000
            if debuglevel >= 1: print 'scale', mappdf_requestdata['pages'][0]['scale']
            break
    mappdf_requestdata['selectedBox'] = urllib.quote(areadata['bbox4point'], ',')
    mappdf_requestdata['mapTitle'] = areadata['title']
    mappdf_requestdata['comment'] = areadata['title']
    mappdf_requestdata['dpi'] = dpi
    mappdf_requestdata['layout'] = '{paper_size} {page_orientation}'.format(**dict(areadata, **{'paper_size':paper_size}))
    if debuglevel >= 3: pprint.pprint(mappdf_requestdata)
    mappdf_response = session2.post('http://asdc.immap.org/geoserver/pdf/create.json', data=json.dumps(mappdf_requestdata))
    if debuglevel >= 2: print 'request make map pdf', mappdf_response
    if mappdf_response.status_code != 200:
        print 'request server to make map pdf failed on {area_type} {area_name} code {area_code}'.format(**areadata)
        if debuglevel >= 1: print 'reason', '\n', mappdf_response.reason
        if debuglevel >= 2: print 'response content', '\n', mappdf_response.content
        return

    # command server to combine map and dashboard pdfs
    multipdf_requestdata_tpl = Template('{"urls":[null,$baseline,$accesibility,null,$floodrisk,null,$avalancherisk,$earthquake,$security,$landslide],"fileName":"$filename","mapTitle":"$mapTitle","mapUrl":"$mapUrl"}')
    mappdf_responsecontent = json.loads(mappdf_response.content)
    multipdf_requestdata = multipdf_requestdata_tpl.substitute(
        mapUrl=mappdf_responsecontent['getURL'],
        mapTitle = areadata['title'],
        filename = urllib.quote(areadata['file_name']),
        baseline='null',
        accesibility='null',
        floodrisk='null',
        avalancherisk='null',
        earthquake='null',
        security='null',
        landslide='null',
        # baseline='"?page=baseline&pdf=true&date={date_now}{multipdf_url_add}"'.format(**areadata),
        # accesibility='"?page=accessibility&pdf=true&date={date_now}{multipdf_url_add}"'.format(**areadata),
        # floodrisk='"?page=floodrisk&pdf=true&date={date_now}{multipdf_url_add}"'.format(**areadata),
        # avalancherisk='"?page=avalancherisk&pdf=true&date={date_now}{multipdf_url_add}"'.format(**areadata),
        # earthquake='"?page=earthquake&pdf=true&date={date_now}{multipdf_url_add}"'.format(**areadata),
        # security='"?page=security&pdf=true&date={date_now}{multipdf_url_add}"'.format(**areadata),
        # landslide='"?page=landslide&pdf=true&date={date_now}{multipdf_url_add}"'.format(**areadata),
        )
    response = session2.post('http://asdc.immap.org/dashboard/multiple', data=multipdf_requestdata, timeout=timeout)

    # get combined pdf
    if debuglevel >= 2: print 'request combine multiple pdf', response
    if response.status_code != 200:
        print 'request server to combine multiple pdf failed on {area_type} {area_name} code {area_code}'.format(**areadata)
        if debuglevel >= 1: print 'reason', '\n', response.reason
        if debuglevel >= 2: print 'response content', '\n', response.content
        return
    combined_pdf_name = json.loads(response.content).get('filename')
    tpl = Template('http://asdc.immap.org/dashboard/downloadPDFFile?filename=$filename&filenameoutput=$filenameoutput')
    url = tpl.substitute(
        filename = combined_pdf_name,
        filenameoutput = areadata['file_name']
        )
    response = session2.get(url)

    return response.content
Exemple #49
0
    def test_thousand_sq_km_aoi(self):
        aoi = GEOSGeometry(json.dumps({
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -96.1083984375,
                        41.12074559016745
                    ],
                    [
                        -95.7513427734375,
                        41.12074559016745
                    ],
                    [
                        -95.7513427734375,
                        41.39741506646461
                    ],
                    [
                        -96.1083984375,
                        41.39741506646461
                    ],
                    [
                        -96.1083984375,
                        41.12074559016745
                    ]
                ]
            ]
        }), srid=4326)
        reprojected_aoi = aoi.transform(5070, clone=True)

        abutting_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -96.18255615234375,
                        41.24064190269475
                    ],
                    [
                        -96.10736846923828,
                        41.24064190269475
                    ],
                    [
                        -96.10736846923828,
                        41.2765163855178
                    ],
                    [
                        -96.18255615234375,
                        41.2765163855178
                    ],
                    [
                        -96.18255615234375,
                        41.24064190269475
                    ]
                ]
            ]
        }

        intersecting_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -95.8172607421875,
                        41.0607151401866
                    ],
                    [
                        -95.68405151367188,
                        41.0607151401866
                    ],
                    [
                        -95.68405151367188,
                        41.160046141686905
                    ],
                    [
                        -95.8172607421875,
                        41.160046141686905
                    ],
                    [
                        -95.8172607421875,
                        41.0607151401866
                    ]
                ]
            ]
        }

        contained_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -95.93811035156249,
                        41.306697618181865
                    ],
                    [
                        -95.82550048828125,
                        41.306697618181865
                    ],
                    [
                        -95.82550048828125,
                        41.3757780692323
                    ],
                    [
                        -95.93811035156249,
                        41.3757780692323
                    ],
                    [
                        -95.93811035156249,
                        41.306697618181865
                    ]
                ]
            ]
        }

        self.assertFalse(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                        abutting_catchment))
        self.assertTrue(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                       intersecting_catchment))
        self.assertTrue(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                       contained_catchment))
Exemple #50
0
        request.user, access_token, * (NON_WMS_BASE_LAYERS + [maplayer])))
    context_dict["preview"] = getattr(
        settings,
        'LAYER_PREVIEW_LIBRARY',
        'leaflet')
    context_dict["crs"] = getattr(
        settings,
        'DEFAULT_MAP_CRS',
        'EPSG:900913')

    # provide bbox in EPSG:4326 for leaflet
    if context_dict["preview"] == 'leaflet':
        srid, wkt = layer.geographic_bounding_box.split(';')
        srid = re.findall(r'\d+', srid)
        geom = GEOSGeometry(wkt, srid=int(srid[0]))
        geom.transform(4326)
        context_dict["layer_bbox"] = ','.join([str(c) for c in geom.extent])

    if layer.storeType == 'dataStore':
        links = layer.link_set.download().filter(
            name__in=settings.DOWNLOAD_FORMATS_VECTOR)
    else:
        links = layer.link_set.download().filter(
            name__in=settings.DOWNLOAD_FORMATS_RASTER)
    links_view = [item for idx, item in enumerate(links) if
                  item.url and 'wms' in item.url or 'gwc' in item.url]
    links_download = [item for idx, item in enumerate(
        links) if item.url and 'wms' not in item.url and 'gwc' not in item.url]
    for item in links_view:
        if item.url and access_token and 'access_token' not in item.url:
            params = {'access_token': access_token}
Exemple #51
0
    def test_sq_km_aoi(self):
        aoi = GEOSGeometry(json.dumps({
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -75.27900695800781,
                        39.891925022904516
                    ],
                    [
                        -75.26608943939209,
                        39.891925022904516
                    ],
                    [
                        -75.26608943939209,
                        39.90173657727282
                    ],
                    [
                        -75.27900695800781,
                        39.90173657727282
                    ],
                    [
                        -75.27900695800781,
                        39.891925022904516
                    ]
                ]
            ]
        }), srid=4326)

        reprojected_aoi = aoi.transform(5070, clone=True)

        abutting_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -75.28535842895508,
                        39.898279646242635
                    ],
                    [
                        -75.27896404266357,
                        39.898279646242635
                    ],
                    [
                        -75.27896404266357,
                        39.90305345750681
                    ],
                    [
                        -75.28535842895508,
                        39.90305345750681
                    ],
                    [
                        -75.28535842895508,
                        39.898279646242635
                    ]
                ]
            ]
        }

        intersecting_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -75.26849269866943,
                        39.890838422106924
                    ],
                    [
                        -75.26244163513184,
                        39.890838422106924
                    ],
                    [
                        -75.26244163513184,
                        39.89498716884207
                    ],
                    [
                        -75.26849269866943,
                        39.89498716884207
                    ],
                    [
                        -75.26849269866943,
                        39.890838422106924
                    ]
                ]
            ]
        }

        contained_catchment = {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -75.27368545532225,
                        39.89722607068418
                    ],
                    [
                        -75.26887893676758,
                        39.89722607068418
                    ],
                    [
                        -75.26887893676758,
                        39.90124274066003
                    ],
                    [
                        -75.27368545532225,
                        39.90124274066003
                    ],
                    [
                        -75.27368545532225,
                        39.89722607068418
                    ]
                ]
            ]
        }

        self.assertFalse(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                        abutting_catchment))
        self.assertTrue(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                       intersecting_catchment))
        self.assertTrue(calcs.catchment_intersects_aoi(reprojected_aoi,
                                                       contained_catchment))
Exemple #52
0
def find_intersects(geography_wkt):

    return_data = {}
    found_intersects = {}
    survey_boundaries = {}
    intersect_data = []

    # We need a geom object from the WKT
    print geography_wkt
    geom_object = GEOSGeometry(geography_wkt, srid=27700)
    print geom_object

    # It's almost always in the wrong SRID, so transform it
    geom_object = geom_object.transform(4326, clone=True)
    print geom_object

    # geometry_columns is a full list of dicts
    # which define a model, its name and table names
    for geoms in geometry_columns:
        try:

            f_table_name = str(geoms['table_name'])
            f_geometry_column = geoms['geometry_column']
            f_label = 'name'
            if 'label' in geoms:
                f_label = geoms['label']

            print '\n', f_table_name, f_geometry_column, f_label

            survey_data = {}
            survey_data['areas'] = []

            spatial_layer_table = geoms['table_model']

            # area_names_query = spatial_layer_table.objects.using('new').extra(
            #     select={
            #         'geometry': 'ST_Intersects(ST_Transform(ST_GeometryFromText("' + geography_wkt + '", 27700), 4326), ' + f_geometry_column +')'
            #     }
            # )

            variable_column = f_geometry_column
            search_type = 'intersects'
            filter_var = variable_column + '__' + search_type
            area_names_queryset = spatial_layer_table.objects.using('new').all().filter(**{filter_var: geom_object})

            # print 'area_names_query', area_names_queryset.query
            area_names = area_names_queryset.values_list(f_label, flat=True)
            # print list(area_names)
            print len(list(area_names))

            if connections['new'].queries:
                print connections['new'].queries[-1]

            if len(list(area_names)) > 0:
                link_table = models.SpatialSurveyLink.objects.filter(geom_table_name=f_table_name).prefetch_related('survey')

                # link_table_surveys = link_table.distinct('survey')
                # print link_table_surveys

                intersect_data.append({
                    'table_name': f_table_name,
                    'intersecting_regions': list(area_names),
                })

                available_options = {}
                for found in link_table:

                    # Create empty list of boundaries if we dont have one for this survey yet
                    if found.survey.identifier not in survey_boundaries:
                        survey_boundaries[found.survey.identifier] = []

                    # Append to list of boundary names "Police', 'AEFA' if not already there
                    if found.boundary_name not in survey_boundaries[found.survey.identifier]:
                        survey_boundaries[found.survey.identifier].append(found.boundary_name)

                    # TODO this makes crazy assumptions
                    if found.survey.identifier not in available_options:
                        available_options[found.survey.identifier] = []

                    # Data available for this geography, for this survey
                    available_options[found.survey.identifier].append(found.data_name)

                found_intersects[f_table_name] = {
                    'table_options': available_options,
                    'intersects': list(area_names)
                }
        except Exception as e69342758432:
            print '**starterror**\nSpatial search error: ', str(e69342758432), type(e69342758432) , '\n**enderror**\n'
            pass
    # print connections['new'].queries

    print 'intersect_data', intersect_data
    return_data['survey_boundaries'] = survey_boundaries
    return_data['boundary_surveys'] = found_intersects
    return_data['intersects'] = intersect_data
    return return_data
Exemple #53
0
    def gridCluster(self, request):

        params = self.loadJson(request)

        clusterGeometries = self.getClusterGeometries(request, params,
                                                      "viewport")

        gridCells = []

        if clusterGeometries:

            filterstring = self.constructFilterstring(params["filters"])

            cursor = connections['default'].cursor()

            cursor.execute('''CREATE TEMPORARY TABLE temp_clusterareas (
                  id serial,
                  polygon geometry
               )''')

            for clusterGeometry in clusterGeometries:
                cursor.execute('''
                    INSERT INTO temp_clusterareas (polygon)
                    ( SELECT (
                        ST_Dump(
                            ST_GeometryFromText('%s')
                    )).geom )
                ''' % clusterGeometry["geos"].ewkt)

            # indexing did not increase performance
            # cursor.execute('''CREATE INDEX temp_gix ON temp_clusterareas USING GIST (polygon);''')

            gridcluster_queryset = '''
                SELECT count(*) AS count, polygon FROM "%s", temp_clusterareas
                WHERE coordinates IS NOT NULL AND ST_Intersects(coordinates, polygon) %s
                GROUP BY polygon
            ''' % (geo_table, filterstring)

            cursor.execute(gridcluster_queryset)

            gridCells_pre = cursor.fetchall()

            for cell in gridCells_pre:

                count = cell[0]

                geos = GEOSGeometry(cell[1])
                geos.transform(self.input_srid)
                centroid = geos.centroid

                cellobj = {
                    "count": count,
                    "geojson": geos.geojson,
                    "center": {
                        "x": centroid.x,
                        "y": centroid.y
                    }
                }

                gridCells.append(cellobj)

        return gridCells
def collect_data(geop_results,
                 geojson,
                 watershed_id=None,
                 weather=None,
                 layer_overrides={}):
    geop_result = {k: v for r in geop_results for k, v in r.items()}

    geom = GEOSGeometry(geojson, srid=4326)
    area = geom.transform(5070, clone=True).area  # Square Meters

    # Data Model is called z by convention
    z = deepcopy(settings.GWLFE_DEFAULTS)

    z['watershed_id'] = watershed_id

    # Statically calculated lookup values
    z['DayHrs'] = day_lengths(geom)

    # Data from the Weather Stations dataset
    if weather is not None:
        ws, wd = weather
    else:
        ws = nearest_weather_stations([(None, watershed_id, geojson)])

    z['WeatherStations'] = [{
        'station': s.station,
        'distance': s.dist
    } for s in ws]

    z['Grow'] = growing_season(ws)
    z['Acoef'] = erosion_coeff(ws, z['Grow'])
    z['PcntET'] = et_adjustment(ws)
    z['WxYrBeg'] = int(max([w.begyear for w in ws]))
    z['WxYrEnd'] = int(min([w.endyear for w in ws]))
    z['WxYrs'] = z['WxYrEnd'] - z['WxYrBeg'] + 1

    # Data from the County Animals dataset
    ag_lscp = ag_ls_c_p(geom)
    z['C'][0] = ag_lscp.hp_c
    z['C'][1] = ag_lscp.crop_c

    livestock_aeu, poultry_aeu, population = animal_energy_units(geom)
    z['AEU'] = livestock_aeu / (area * ACRES_PER_SQM)
    z['n41j'] = livestock_aeu
    z['n41k'] = poultry_aeu
    z['n41l'] = livestock_aeu + poultry_aeu
    z['NumAnimals'] = [
        int(population.get(animal, 0)) for animal in ANIMAL_KEYS
    ]

    z['ManNitr'], z['ManPhos'] = manure_spread(z['AEU'])

    # Data from Streams dataset
    datasource = get_layer_value('__STREAMS__', layer_overrides)
    z['StreamLength'] = stream_length(geom, datasource) or 10  # Meters
    z['n42b'] = round(z['StreamLength'] / 1000, 1)  # Kilometers

    # Data from Point Source Discharge dataset
    n_load, p_load, discharge = point_source_discharge(geom,
                                                       area,
                                                       drb=geom.within(DRB))
    z['PointNitr'] = n_load
    z['PointPhos'] = p_load
    z['PointFlow'] = discharge

    # Data from National Weather dataset
    if weather is None:
        wd = weather_data(ws, z['WxYrBeg'], z['WxYrEnd'])
        temps_dict, prcps_dict = wd
        temps = average_weather_data(list(temps_dict.values()))
        prcps = average_weather_data(list(prcps_dict.values()))
    else:
        temps, prcps = wd
    z['Temp'] = temps
    z['Prec'] = prcps

    # Begin processing geop_result

    # Set stream related variables to zero if AoI does not contain
    # any streams.
    if 'ag_stream_pct' in geop_result:
        z['AgLength'] = geop_result['ag_stream_pct'] * z['StreamLength']
        z['UrbLength'] = z['StreamLength'] - z['AgLength']
        z['n42'] = round(z['AgLength'] / 1000, 1)
        z['n46e'] = (geop_result['med_high_urban_stream_pct'] *
                     z['StreamLength'] / 1000)
        z['n46f'] = (geop_result['low_urban_stream_pct'] * z['StreamLength'] /
                     1000)
    else:
        z['AgLength'] = 0
        z['UrbLength'] = 0
        z['n42'] = 0
        z['n46e'] = 0
        z['n46f'] = 0

    z['CN'] = geop_result['cn']
    z['SedPhos'] = geop_result['soilp']
    z['Area'] = [
        percent * area * HECTARES_PER_SQM
        for percent in geop_result['landuse_pcts']
    ]

    # Immediately return an error if z['Area'] is a list of 0s
    if sum(z['Area']) == 0:
        raise Exception(NO_LAND_COVER)

    z['PhosConc'] = phosphorus_conc(z['SedPhos'])

    z['AgSlope3'] = geop_result['ag_slope_3_pct'] * area * HECTARES_PER_SQM
    z['AgSlope3To8'] = (geop_result['ag_slope_3_8_pct'] * area *
                        HECTARES_PER_SQM)
    z['n41'] = geop_result['n41']

    z['AvSlope'] = geop_result['avg_slope']

    z['AvKF'] = geop_result['avg_kf']
    z['KF'] = geop_result['kf']

    z['SedDelivRatio'] = sediment_delivery_ratio(area * SQKM_PER_SQM)
    z['TotArea'] = area * HECTARES_PER_SQM
    z['GrNitrConc'] = geop_result['gr_nitr_conc']
    z['GrPhosConc'] = geop_result['gr_phos_conc']
    z['MaxWaterCap'] = geop_result['avg_awc']

    # Calculate fields derived from land use distribution
    z = area_calculations(z['Area'], z)

    # Use zeroed out stream variables if there are no streams in the AoI
    if 'lu_stream_pct' in geop_result:
        z['LS'] = ls_factors(geop_result['lu_stream_pct'], z['StreamLength'],
                             z['Area'], z['AvSlope'], ag_lscp)
    else:
        zeroed_lu_stream_pct = [0.0] * 16
        z['LS'] = ls_factors(zeroed_lu_stream_pct, 0, z['Area'], z['AvSlope'],
                             ag_lscp)

    z['P'] = p_factors(z['AvSlope'], ag_lscp)

    z['SedNitr'] = geop_result['soiln']

    z['RecessionCoef'] = geop_result['recess_coef']

    return z
Exemple #55
0
    def handle(self, *args, **options):
        if len(args) == 0:
            raise CommandError("Pass me the shapefile path")

        print "Assuming shapefile uses srid/epsg 4269!"

        # read the shapefile
        sf = shapefile.Reader(args[0])

        # make the cursor work with postgres hstore fields
        cursor = connection.cursor()
        extras.register_hstore(cursor.cursor)

        with transaction.atomic():
            # we want to disable the trigger that updated the changed_on hstore
            # field, since we don't care when the NHD changes its own data
            cursor.execute("ALTER TABLE nhd DISABLE TRIGGER USER")

            # the first field is the DeleteFlg, which isn't stored in the actual
            # records, so we cut it off
            fields = sf.fields[1:]
            # the fields *should* be labeled as such, so we use these keys to index
            # the record dictionary
            #['Permanent_', 'FDate', 'Resolution', 'GNIS_ID', 'GNIS_Name',
            # 'AreaSqKm', 'Elevation', 'ReachCode', 'FType', 'FCode', 'Shape_Leng',
            # 'Shape_Area', 'AOLReachCo', 'AOLGNIS_Na']

            # get the fields out of our model since we may not want to update
            # every one of them. We need to remove the PK though, since it
            # causes problems for Django
            relevant_fields = set(f.name for f in NHDLake._meta.fields) - set(
                ['reachcode'])

            for shape, record in itertools.izip(sf.iterShapes(),
                                                sf.iterRecords()):
                # make it so we can index by column name instead of column position
                record = dict(
                    (field_description[0], field_value)
                    for field_description, field_value in zip(fields, record))
                if record['ReachCode'].strip() == "":
                    print "Skipping lake with no reachcode and permanent_id=%s" % record[
                        'Permanent_'].strip()

                # fetch the existing hstore for the lake, which tells us when
                # each column was modified
                cursor.execute(
                    """SELECT changed_on FROM nhd WHERE reachcode = %s""",
                    (record['ReachCode'], ))
                row = cursor.fetchone()
                if row is not None:
                    # remove any fields that have been modified outside of the
                    # NHD (i.e. someone edited the lake in the database
                    # manually), since we don't want overwrite those
                    modified_fields = set(row[0].keys())
                    update_fields = relevant_fields - modified_fields
                else:
                    modified_fields = set()
                    # this will cause an insert query to be performed
                    update_fields = None

                NHDLake(reachcode=record['ReachCode'].strip(),
                        permanent_id=record['Permanent_'].strip(),
                        fdate=datetime.date(*record['FDate']),
                        ftype=record['FType'],
                        fcode=record['FCode'],
                        shape_length=float(record['Shape_Leng']),
                        shape_area=float(record['Shape_Area']),
                        resolution=record['Resolution'],
                        gnis_id=record['GNIS_ID'].strip(),
                        gnis_name=record['GNIS_Name'].strip(),
                        area_sq_km=float(record['AreaSqKm']),
                        elevation=float(record['Elevation'])).save(
                            update_fields=update_fields)

                # only change the geom if it has been changed already
                if "the_geom" not in modified_fields:
                    # this stupid pyshp library has no way to spit out the wkt
                    # which is what GEOSGeometry needs, so we have to rely on
                    # another library to do the conversion
                    geom = GEOSGeometry(asShape(shape).wkt, srid=4269)
                    # cast Polygons to Multipolygons
                    if geom.geom_type == "Polygon":
                        geom = MultiPolygon(geom, srid=4269)
                    geom.transform(3644)  # transform to the proper epsg code
                    # update the Geometry
                    LakeGeom(reachcode=record['ReachCode'].strip(),
                             the_geom=geom).save()

            cursor.execute("ALTER TABLE nhd ENABLE TRIGGER USER")
Exemple #56
0
def collect_data(geop_result, geojson):
    geom = GEOSGeometry(geojson, srid=4326)
    area = geom.transform(5070, clone=True).area  # Square Meters

    # Data Model is called z by convention
    z = settings.GWLFE_DEFAULTS.copy()

    # Statically calculated lookup values
    z['DayHrs'] = day_lengths(geom)

    # Data from the Weather Stations dataset
    ws = nearest_weather_stations(geom)
    z['Grow'] = growing_season(ws)
    z['Acoef'] = erosion_coeff(ws, z['Grow'])
    z['PcntET'] = et_adjustment(ws)
    z['WxYrBeg'] = int(max([w.begyear for w in ws]))
    z['WxYrEnd'] = int(min([w.endyear for w in ws]))
    z['WxYrs'] = z['WxYrEnd'] - z['WxYrBeg'] + 1

    # Data from the County Animals dataset
    ag_lscp = ag_ls_c_p(geom)
    z['C'][0] = ag_lscp.hp_c
    z['C'][1] = ag_lscp.crop_c

    livestock_aeu, poultry_aeu, population = animal_energy_units(geom)
    z['AEU'] = livestock_aeu / (area * ACRES_PER_SQM)
    z['n41j'] = livestock_aeu
    z['n41k'] = poultry_aeu
    z['n41l'] = livestock_aeu + poultry_aeu
    z['NumAnimals'] = [
        int(population.get(animal, 0)) for animal in ANIMAL_KEYS
    ]

    z['ManNitr'], z['ManPhos'] = manure_spread(z['AEU'])

    # Data from Streams dataset
    z['StreamLength'] = stream_length(geom)  # Meters
    z['n42b'] = round(z['StreamLength'] / 1000, 1)  # Kilometers

    # Data from Point Source Discharge dataset
    n_load, p_load, discharge = point_source_discharge(geom,
                                                       area,
                                                       drb=geom.within(DRB))
    z['PointNitr'] = n_load
    z['PointPhos'] = p_load
    z['PointFlow'] = discharge

    # Data from National Weather dataset
    temps, prcps = weather_data(ws, z['WxYrBeg'], z['WxYrEnd'])
    z['Temp'] = temps
    z['Prec'] = prcps

    # Begin processing geop_result
    z['AgLength'] = geop_result['ag_stream_pct'] * z['StreamLength']
    z['UrbLength'] = z['StreamLength'] - z['AgLength']
    z['n42'] = round(z['AgLength'] / 1000, 1)
    z['n46e'] = (geop_result['med_high_urban_stream_pct'] * z['StreamLength'] /
                 1000)
    z['n46f'] = geop_result['low_urban_stream_pct'] * z['StreamLength'] / 1000

    z['CN'] = geop_result['cn']
    z['SedPhos'] = geop_result['sed_phos']
    z['Area'] = [
        percent * area * HECTARES_PER_SQM
        for percent in geop_result['landuse_pcts']
    ]

    # Immediately return an error if z['Area'] is a list of 0s
    if sum(z['Area']) == 0:
        raise Exception(NO_LAND_COVER)

    z['UrbAreaTotal'] = sum(z['Area'][NRur:])
    z['PhosConc'] = phosphorus_conc(z['SedPhos'])

    z['NumNormalSys'] = num_normal_sys(z['Area'])

    z['AgSlope3'] = geop_result['ag_slope_3_pct'] * area * HECTARES_PER_SQM
    z['AgSlope3To8'] = (geop_result['ag_slope_3_8_pct'] * area *
                        HECTARES_PER_SQM)
    z['n41'] = geop_result['n41']

    z['AvSlope'] = geop_result['avg_slope']

    z['AvKF'] = geop_result['avg_kf']
    z['KF'] = geop_result['kf']

    z['KV'] = kv_coefficient(geop_result['landuse_pcts'], z['Grow'])

    # Original at [email protected]:9803-9807
    z['n23'] = z['Area'][1]  # Row Crops Area
    z['n23b'] = z['Area'][13]  # High Density Mixed Urban Area
    z['n24'] = z['Area'][0]  # Hay/Pasture Area
    z['n24b'] = z['Area'][11]  # Low Density Mixed Urban Area

    z['SedDelivRatio'] = sediment_delivery_ratio(area * SQKM_PER_SQM)
    z['TotArea'] = area * HECTARES_PER_SQM
    z['GrNitrConc'] = geop_result['gr_nitr_conc']
    z['GrPhosConc'] = geop_result['gr_phos_conc']
    z['MaxWaterCap'] = geop_result['avg_awc']
    z['SedAFactor'] = sed_a_factor(geop_result['landuse_pcts'], z['CN'],
                                   z['AEU'], z['AvKF'], z['AvSlope'])

    z['LS'] = ls_factors(geop_result['lu_stream_pct'], z['StreamLength'],
                         z['Area'], z['AvSlope'], ag_lscp)

    z['P'] = p_factors(z['AvSlope'], ag_lscp)

    return z
Exemple #57
0
 def test_should_filter_queryset_intersecting_shape(self):
     shape = GEOSGeometry('POLYGON((0 -1, 4 -1, 4 1, 0 1, 0 -1))',
                          srid=settings.SRID)
     shape.transform(API_SRID)
     result = self.filter.filter(self.model.objects.all(), shape)
     self.assertEqual(2, len(result))
Exemple #58
0
def pagination():
    session = requests.Session()
    session.headers.update({
        'User-Agent':
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36'
    })
    tokenRequest = session.get('https://www.funda.nl/mijn/login/')

    request_validation_re = re.compile(
        r'<input name="__RequestVerificationToken" type="hidden" value="(.*?)" />'
    )
    tokens = request_validation_re.findall(tokenRequest.text)

    sessionCookies = tokenRequest.cookies

    payload = {
        '__RequestVerificationToken': tokens[0],
        'Username': PASSWD,
        'Password': USER,
        'RememberMe': 'false'
    }

    raw = urllib.urlencode(payload)
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
    }
    session.post('https://www.funda.nl/mijn/login/',
                 data=raw,
                 cookies=sessionCookies,
                 headers=headers)

    links = list()
    resp = session.get(BEWRD_URL + 'p1')
    soup = BeautifulSoup(resp.text)

    pagelinks = soup.find_all("a", attrs={"data-pagination-pagelink": True})
    pages = []
    for page in pagelinks:
        pages.append(int(page["data-pagination-pagelink"]))
    einde = max(pages) + 1

    for i in range(1, einde):
        links.append(BEWRD_URL + 'p' + str(i))

    for page in links:
        html = session.get(page)
        soup = BeautifulSoup(html.text)
        houses = soup.find('ul',
                           class_='search-results saved-objects').find_all(
                               'div', class_="search-result-content-inner")
        for house in houses:
            raw_address = house.find('h3', class_='search-result-title').text
            raw_address_list = [s.strip() for s in raw_address.splitlines()]
            street_nr = raw_address_list[1]
            postalcode_city = raw_address_list[3]
            price = int(
                unidecode.unidecode(
                    house.find('span',
                               class_='search-result-price').text).replace(
                                   "EUR ", "").replace(".",
                                                       "").replace("kk", ""))

            pcode = street_nr + " " + postalcode_city
            url = GEOCODE_URL + pcode
            response = requests.get(url)
            try:
                # see http://gis.stackexchange.com/questions/58271/using-python-to-parse-an-xml-containing-gml-tags
                root = ET.fromstring(response.content)
                for point in root.findall(
                        './/{http://www.opengis.net/gml}Point'):
                    rdxy = point.findtext(
                        "{http://www.opengis.net/gml}pos").split()
                pnt = GEOSGeometry('POINT({0} {1})'.format(rdxy[0], rdxy[1]),
                                   srid=28992)
                # see http://gis.stackexchange.com/questions/94640/geodjango-transform-not-working
                pnt.transform(4326)
            except:
                rdxy = [0, 0]
                pnt = GEOSGeometry('POINT({0} {1})'.format(0, 0), srid=4326)

            House.objects.get_or_create(fuid=0,
                                        vraagprijs=price,
                                        postcode=postalcode_city,
                                        link='http://test',
                                        rdx=rdxy[0],
                                        rdy=rdxy[1],
                                        geom=pnt)
Exemple #59
0
 def test_transform_3d(self):
     p3d = GEOSGeometry('POINT (5 23 100)', 4326)
     p3d.transform(2774)
     self.assertEqual(p3d.z, 100)
Exemple #60
0
 def read_manual(self, fn):
     dir = os.path.dirname(__file__) + '/../..'
     fp = open('%s/data/2019-%s.geojson' % (dir, fn))
     p = GEOSGeometry(fp.read(), srid=4326)
     p.transform(27700)
     return p