예제 #1
0
파일: tests.py 프로젝트: 5977862/django
    def test_snap_to_grid(self):
        "Testing GeoQuerySet.snap_to_grid()."
        # Let's try and break snap_to_grid() with bad combinations of arguments.
        for bad_args in ((), range(3), range(5)):
            self.assertRaises(ValueError, Country.objects.snap_to_grid, *bad_args)
        for bad_args in (('1.0',), (1.0, None), tuple(map(six.text_type, range(4)))):
            self.assertRaises(TypeError, Country.objects.snap_to_grid, *bad_args)

        # Boundary for San Marino, courtesy of Bjorn Sandvik of thematicmapping.org
        # from the world borders dataset he provides.
        wkt = ('MULTIPOLYGON(((12.41580 43.95795,12.45055 43.97972,12.45389 43.98167,'
               '12.46250 43.98472,12.47167 43.98694,12.49278 43.98917,'
               '12.50555 43.98861,12.51000 43.98694,12.51028 43.98277,'
               '12.51167 43.94333,12.51056 43.93916,12.49639 43.92333,'
               '12.49500 43.91472,12.48778 43.90583,12.47444 43.89722,'
               '12.46472 43.89555,12.45917 43.89611,12.41639 43.90472,'
               '12.41222 43.90610,12.40782 43.91366,12.40389 43.92667,'
               '12.40500 43.94833,12.40889 43.95499,12.41580 43.95795)))')
        Country.objects.create(name='San Marino', mpoly=fromstr(wkt))

        # Because floating-point arithmetic isn't exact, we set a tolerance
        # to pass into GEOS `equals_exact`.
        tol = 0.000000001

        # SELECT AsText(ST_SnapToGrid("geoapp_country"."mpoly", 0.1)) FROM "geoapp_country" WHERE "geoapp_country"."name" = 'San Marino';
        ref = fromstr('MULTIPOLYGON(((12.4 44,12.5 44,12.5 43.9,12.4 43.9,12.4 44)))')
        self.assertTrue(ref.equals_exact(Country.objects.snap_to_grid(0.1).get(name='San Marino').snap_to_grid, tol))

        # SELECT AsText(ST_SnapToGrid("geoapp_country"."mpoly", 0.05, 0.23)) FROM "geoapp_country" WHERE "geoapp_country"."name" = 'San Marino';
        ref = fromstr('MULTIPOLYGON(((12.4 43.93,12.45 43.93,12.5 43.93,12.45 43.93,12.4 43.93)))')
        self.assertTrue(ref.equals_exact(Country.objects.snap_to_grid(0.05, 0.23).get(name='San Marino').snap_to_grid, tol))

        # SELECT AsText(ST_SnapToGrid("geoapp_country"."mpoly", 0.5, 0.17, 0.05, 0.23)) FROM "geoapp_country" WHERE "geoapp_country"."name" = 'San Marino';
        ref = fromstr('MULTIPOLYGON(((12.4 43.87,12.45 43.87,12.45 44.1,12.5 44.1,12.5 43.87,12.45 43.87,12.4 43.87)))')
        self.assertTrue(ref.equals_exact(Country.objects.snap_to_grid(0.05, 0.23, 0.5, 0.17).get(name='San Marino').snap_to_grid, tol))
예제 #2
0
    def test_point_on_surface(self):
        # Reference values.
        if oracle:
            # SELECT SDO_UTIL.TO_WKTGEOMETRY(SDO_GEOM.SDO_POINTONSURFACE(GEOAPP_COUNTRY.MPOLY, 0.05))
            # FROM GEOAPP_COUNTRY;
            ref = {
                'New Zealand':
                fromstr('POINT (174.616364 -36.100861)', srid=4326),
                'Texas':
                fromstr('POINT (-103.002434 36.500397)', srid=4326),
            }
        else:
            # Using GEOSGeometry to compute the reference point on surface values
            # -- since PostGIS also uses GEOS these should be the same.
            ref = {
                'New Zealand':
                Country.objects.get(name='New Zealand').mpoly.point_on_surface,
                'Texas':
                Country.objects.get(name='Texas').mpoly.point_on_surface
            }

        qs = Country.objects.annotate(
            point_on_surface=functions.PointOnSurface('mpoly'))
        for country in qs:
            tol = 0.00001  # Spatialite might have WKT-translation-related precision issues
            self.assertTrue(ref[country.name].equals_exact(
                country.point_on_surface, tol))
예제 #3
0
def markers_within_bounds(request):
    swLat = request.GET.get('swLat')
    swLng = request.GET.get('swLng')
    neLat = request.GET.get('neLat')
    neLng = request.GET.get('neLng')
    centreLat = str(request.GET.get('centreLat'))
    centreLng = str(request.GET.get('centreLng'))
    centre = fromstr('POINT(%s %s)' % (centreLng, centreLat))
    boundaries = fromstr('POLYGON((%s %s,%s %s,%s %s,%s %s,%s %s))' % \
           (swLng, swLat, swLng, neLat, neLng, neLat, neLng, swLat, swLng, swLat))
    places = Place.objects.filter(location__within=boundaries).distance(centre).order_by('distance')
    markers = []
    for place in places:
        place_dict = {}
        place_dict['lat'] = place.location.y
        place_dict['lng'] = place.location.x
        place_dict['grid'] = place.grid
        place_dict['vill'] = place.vill
        place_dict['vill_slug'] = place.vill_slug
        if place.hundred:
            place_dict['hundred'] = place.hundred.name
            place_dict['hundred_slug'] = place.hundred.name_slug
        county = place.county.all()[0].name
        place_dict['county'] = county
        place_dict['value'] = place.value
        place_dict['distance'] = str(place.distance/1000).rstrip(" m")
        place_dict['raw_value'] = place.raw_value
        place_dict['population'] = place.population
        markers.append(place_dict)
    json = simplejson.dumps(markers)
    return HttpResponse(json, mimetype='application/json')
예제 #4
0
    def test_srid(self):
        "Testing the SRID property and keyword."
        # Testing SRID keyword on Point
        pnt = Point(5, 23, srid=4326)
        self.assertEqual(4326, pnt.srid)
        pnt.srid = 3084
        self.assertEqual(3084, pnt.srid)
        self.assertRaises(ctypes.ArgumentError, pnt.set_srid, "4326")

        # Testing SRID keyword on fromstr(), and on Polygon rings.
        poly = fromstr(self.geometries.polygons[1].wkt, srid=4269)
        self.assertEqual(4269, poly.srid)
        for ring in poly:
            self.assertEqual(4269, ring.srid)
        poly.srid = 4326
        self.assertEqual(4326, poly.shell.srid)

        # Testing SRID keyword on GeometryCollection
        gc = GeometryCollection(Point(5, 23), LineString((0, 0), (1.5, 1.5), (3, 3)), srid=32021)
        self.assertEqual(32021, gc.srid)
        for i in range(len(gc)):
            self.assertEqual(32021, gc[i].srid)

        # GEOS may get the SRID from HEXEWKB
        # 'POINT(5 23)' at SRID=4326 in hex form -- obtained from PostGIS
        # using `SELECT GeomFromText('POINT (5 23)', 4326);`.
        hex = "0101000020E610000000000000000014400000000000003740"
        p1 = fromstr(hex)
        self.assertEqual(4326, p1.srid)

        p2 = fromstr(p1.hex)
        self.assertIsNone(p2.srid)
        p3 = fromstr(p1.hex, srid=-1)  # -1 is intended.
        self.assertEqual(-1, p3.srid)
예제 #5
0
    def clean(self):
        cleaned_data = super(MeetingFilters, self).clean()
        center = cleaned_data.get('center')
        radius = cleaned_data.get('radius')

        # Center and radius must be set together
        if center and not radius:
            msg = u'Radius is required when center is specified.'
            self._errors['radius'] = self.error_class([msg])

        if radius and not center:
            msg = u'Center is required when radius is specified.'
            self._errors['center'] = self.error_class([msg])

        if center:
            try:
                center = cleaned_data['center'] = geos.fromstr(center)
            except geos.GEOSException:
                msg = u'Center is not a valid WKT point (see http://en.wikipedia.org/wiki/Well-known_text)'
                self._errors['center'] = self.error_class([msg])

        # N, S, E, and W must all be set together, or none at all.
        bbox = cleaned_data.get('bbox')
        if bbox:
            try:
                n, e, s, w = [float(val) for val in cleaned_data['bbox'].split(',')]
                bbox = u'POLYGON(({e} {n}, {e} {s}, {w} {s}, {w} {n}, {e} {n}))'.format(n=n, e=e, s=s, w=w)
            except ValueError:
                msg = u'Bbox must have exactly 4 comma-separated values.'
                self._errors['bbox'] = self.error_class([msg])
            else:
                cleaned_data['bbox'] = geos.fromstr(bbox)

        return cleaned_data
예제 #6
0
def spike_ring_indecies(line_ring,threshold=0.01):
    """
    Returns a list of point indexes if ring contains spikes (angles of less than threshold degrees).
    Otherwise, an empty list.
    """
    radian_thresh = threshold * (pi/180)
    spike_indecies = []
    for i,pnt in enumerate(line_ring.coords):
        if(i==0 and line_ring.num_points > 3): # The first point  ...which also equals the last point
            p1_coords = line_ring.coords[len(line_ring.coords) - 2]
        elif(i==line_ring.num_points-1): # The first and last point are the same in a line ring so we're done
            break
        else:
            p1_coords = line_ring.coords[i - 1]
            
        # set up the points for the angle test.
        p1_str = 'POINT (%f %f), %i' % (p1_coords[0], p1_coords[1], settings.GEOMETRY_DB_SRID)
        p1 = fromstr(p1_str)
        p2_str = 'POINT (%f %f), %i' % (pnt[0],pnt[1],settings.GEOMETRY_DB_SRID)
        p2 = fromstr(p2_str)
        p3_coords = line_ring.coords[i + 1]
        p3_str = 'POINT (%f %f), %i' % (p3_coords[0], p3_coords[1], settings.GEOMETRY_DB_SRID)
        p3 = fromstr(p3_str)
        if( angle(p1,p2,p3) <= radian_thresh ):
            spike_indecies.append(i)
    
    return spike_indecies
예제 #7
0
    def setUp(self):
        ds = Dataset.objects.get(pk=3)
        self.mp = MapPoint.objects.get(pk=1)
        setattr(self.mp, 'dataset', ds)
        self.mp.point = Point(5, 23)
        MapPoint(lat=23.41, lon=98.0, dataset=ds, point=Point(5, 23), state='NY', city='NYC').save()
        tag1 = Tag(dataset=ds, tag='tag1', approved=True, count=1)
        tag1.save()
        tag2 = Tag(dataset=ds, tag='tag2', approved=True, count=1)
        tag2.save()
        tagindiv1 = TagIndiv(tag=tag1, mapelement=self.mp)
        tagindiv1.save()
        tagindiv2 = TagIndiv(tag=tag2, mapelement=self.mp)
        tagindiv2.save()
        p1 = GEOSGeometry("SRID=4326;POLYGON ((5 23.00902982868961, 5.004877257506506 23.00781998654152, 5.00844745207902 23.00451469036915, 5.009753953024734 22.99999969967893, 5.008446890194161 22.99548485657674, 5.004876695621637 22.99217985558042, 5 22.99097016102037, 4.995123304378363 22.99217985558042, 4.991553109805839 22.99548485657674, 4.990246046975266 22.99999969967893, 4.99155254792098 23.00451469036915, 4.995122742493493 23.00781998654153, 5 23.00902982868961))")
        p2 = GEOSGeometry("SRID=4326;POLYGON ((5 23.02708945518625, 5.014633459242311 23.02345948568706, 5.025344042308789 23.01354271143227, 5.029261858728946 22.99999729711045, 5.025338985345226 22.98645321108062, 5.014628402277986 22.97653909341024, 5 22.97291045220348, 4.985371597722013 22.97653909341024, 4.974661014654773 22.98645321108062, 4.970738141271054 22.99999729711045, 4.974655957691211 23.01354271143227, 4.985366540757689 23.02345948568706, 5 23.02708945518625))")
        p3 = GEOSGeometry("SRID=4326;POLYGON ((5 23.0451490404854, 5.024391911722986 23.03909835241012, 5.042242881329157 23.02256891874033, 5.048769763397385 22.99999249197385, 5.042228834209113 22.97741975490609, 5.024377864597061 22.96089770063709, 5 22.95485070226389, 4.975622135402939 22.96089770063709, 4.957771165790887 22.97741975490609, 4.951230236602615 22.99999249197385, 4.957757118670842 23.02256891874033, 4.975608088277014 23.03909835241012, 5 23.0451490404854))")
        mpoly1 = MultiPolygon(fromstr(str(p1)))
        polygon1 = MapPolygon(lat='50', lon='22', field1=1.0, field2=2.0, mpoly=mpoly1, dataset=ds, remote_id=10)
        polygon1.save()
        mpoly2 = MultiPolygon(fromstr(str(p2)))
        polygon2 = MapPolygon(lat='12', lon='17', field1=1.0, field2=2.0, mpoly=mpoly2, dataset=ds, remote_id=9)
        polygon2.save()
        mpoly3 = MultiPolygon(fromstr(str(p3)))
        polygon3 = MapPolygon(lat='23', lon='27', field1=1.0, field2=2.0, mpoly=mpoly3, dataset=ds, remote_id=9)
        polygon3.save()

        self.request = HttpRequest()
        qdict = QueryDict('', mutable=True)
        qdict.update({'year': '2014'})
        qdict.update({'year': '2016'})
        qdict.update({'unit': 'km'})
        self.request.GET = qdict
        self.serializer = AnalyzeAreaNoValuesSerializer(context={'request': self.request})
예제 #8
0
    def test_linestring(self):
        "Testing LineString objects."
        prev = fromstr('POINT(0 0)')
        for l in self.geometries.linestrings:
            ls = fromstr(l.wkt)
            self.assertEqual(ls.geom_type, 'LineString')
            self.assertEqual(ls.geom_typeid, 1)
            self.assertEqual(ls.dims, 1)
            self.assertEqual(ls.empty, False)
            self.assertEqual(ls.ring, False)
            if hasattr(l, 'centroid'):
                self.assertEqual(l.centroid, ls.centroid.tuple)
            if hasattr(l, 'tup'):
                self.assertEqual(l.tup, ls.tuple)

            self.assertEqual(ls, fromstr(l.wkt))
            self.assertEqual(False, ls == prev)  # Use assertEqual to test __eq__
            self.assertRaises(IndexError, ls.__getitem__, len(ls))
            prev = ls

            # Creating a LineString from a tuple, list, and numpy array
            self.assertEqual(ls, LineString(ls.tuple))  # tuple
            self.assertEqual(ls, LineString(*ls.tuple))  # as individual arguments
            self.assertEqual(ls, LineString([list(tup) for tup in ls.tuple]))  # as list
            # Point individual arguments
            self.assertEqual(ls.wkt, LineString(*tuple(Point(tup) for tup in ls.tuple)).wkt)
            if numpy:
                self.assertEqual(ls, LineString(numpy.array(ls.tuple)))  # as numpy array

            with self.assertRaisesMessage(TypeError, 'Each coordinate should be a sequence (list or tuple)'):
                LineString((0, 0))

            with self.assertRaisesMessage(TypeError, 'LineString requires at least 2 points, got 1.'):
                LineString([(0, 0)])
 def test_unionagg(self):
     """
     Testing the (deprecated) `unionagg` (aggregate union) GeoQuerySet method
     and the Union aggregate.
     """
     tx = Country.objects.get(name='Texas').mpoly
     # Houston, Dallas -- Ordering may differ depending on backend or GEOS version.
     union1 = fromstr('MULTIPOINT(-96.801611 32.782057,-95.363151 29.763374)')
     union2 = fromstr('MULTIPOINT(-95.363151 29.763374,-96.801611 32.782057)')
     qs = City.objects.filter(point__within=tx)
     self.assertRaises(TypeError, qs.unionagg, 'name')
     self.assertRaises(ValueError, qs.aggregate, Union('name'))
     # Using `field_name` keyword argument in one query and specifying an
     # order in the other (which should not be used because this is
     # an aggregate method on a spatial column)
     u1 = qs.unionagg(field_name='point')
     u2 = qs.order_by('name').unionagg()
     u3 = qs.aggregate(Union('point'))['point__union']
     u4 = qs.order_by('name').aggregate(Union('point'))['point__union']
     tol = 0.00001
     self.assertTrue(union1.equals_exact(u1, tol) or union2.equals_exact(u1, tol))
     self.assertTrue(union1.equals_exact(u2, tol) or union2.equals_exact(u2, tol))
     self.assertTrue(union1.equals_exact(u3, tol) or union2.equals_exact(u3, tol))
     self.assertTrue(union1.equals_exact(u4, tol) or union2.equals_exact(u4, tol))
     qs = City.objects.filter(name='NotACity')
     self.assertIsNone(qs.unionagg(field_name='point'))
     self.assertIsNone(qs.aggregate(Union('point'))['point__union'])
예제 #10
0
    def test_polygons(self):
        "Testing Polygon objects."

        prev = fromstr('POINT(0 0)')
        for p in self.geometries.polygons:
            # Creating the Polygon, testing its properties.
            poly = fromstr(p.wkt)
            self.assertEqual(poly.geom_type, 'Polygon')
            self.assertEqual(poly.geom_typeid, 3)
            self.assertEqual(poly.dims, 2)
            self.assertEqual(poly.empty, False)
            self.assertEqual(poly.ring, False)
            self.assertEqual(p.n_i, poly.num_interior_rings)
            self.assertEqual(p.n_i + 1, len(poly))  # Testing __len__
            self.assertEqual(p.n_p, poly.num_points)

            # Area & Centroid
            self.assertAlmostEqual(p.area, poly.area, 9)
            self.assertAlmostEqual(p.centroid[0], poly.centroid.tuple[0], 9)
            self.assertAlmostEqual(p.centroid[1], poly.centroid.tuple[1], 9)

            # Testing the geometry equivalence
            self.assertEqual(poly, fromstr(p.wkt))
            # Should not be equal to previous geometry
            self.assertEqual(False, poly == prev)  # Use assertEqual to test __eq__
            self.assertNotEqual(poly, prev)  # Use assertNotEqual to test __ne__

            # Testing the exterior ring
            ring = poly.exterior_ring
            self.assertEqual(ring.geom_type, 'LinearRing')
            self.assertEqual(ring.geom_typeid, 2)
            if p.ext_ring_cs:
                self.assertEqual(p.ext_ring_cs, ring.tuple)
                self.assertEqual(p.ext_ring_cs, poly[0].tuple)  # Testing __getitem__

            # Testing __getitem__ and __setitem__ on invalid indices
            self.assertRaises(IndexError, poly.__getitem__, len(poly))
            self.assertRaises(IndexError, poly.__setitem__, len(poly), False)
            self.assertRaises(IndexError, poly.__getitem__, -1 * len(poly) - 1)

            # Testing __iter__
            for r in poly:
                self.assertEqual(r.geom_type, 'LinearRing')
                self.assertEqual(r.geom_typeid, 2)

            # Testing polygon construction.
            self.assertRaises(TypeError, Polygon, 0, [1, 2, 3])
            self.assertRaises(TypeError, Polygon, 'foo')

            # Polygon(shell, (hole1, ... holeN))
            rings = tuple(r for r in poly)
            self.assertEqual(poly, Polygon(rings[0], rings[1:]))

            # Polygon(shell_tuple, hole_tuple1, ... , hole_tupleN)
            ring_tuples = tuple(r.tuple for r in poly)
            self.assertEqual(poly, Polygon(*ring_tuples))

            # Constructing with tuples of LinearRings.
            self.assertEqual(poly.wkt, Polygon(*tuple(r for r in poly)).wkt)
            self.assertEqual(poly.wkt, Polygon(*tuple(LinearRing(r.tuple) for r in poly)).wkt)
예제 #11
0
파일: tests.py 프로젝트: kmtracey/django
    def test_point_on_surface(self):
        "Testing the `point_on_surface` GeoQuerySet method."
        # Reference values.
        if oracle:
            # SELECT SDO_UTIL.TO_WKTGEOMETRY(SDO_GEOM.SDO_POINTONSURFACE(GEOAPP_COUNTRY.MPOLY, 0.05)) FROM GEOAPP_COUNTRY;
            ref = {
                "New Zealand": fromstr("POINT (174.616364 -36.100861)", srid=4326),
                "Texas": fromstr("POINT (-103.002434 36.500397)", srid=4326),
            }

        elif postgis or spatialite:
            # Using GEOSGeometry to compute the reference point on surface values
            # -- since PostGIS also uses GEOS these should be the same.
            ref = {
                "New Zealand": Country.objects.get(name="New Zealand").mpoly.point_on_surface,
                "Texas": Country.objects.get(name="Texas").mpoly.point_on_surface,
            }

        for c in Country.objects.point_on_surface():
            if spatialite:
                # XXX This seems to be a WKT-translation-related precision issue?
                tol = 0.00001
            else:
                tol = 0.000000001
            self.assertEqual(True, ref[c.name].equals_exact(c.point_on_surface, tol))
예제 #12
0
    def test_linestring(self):
        "Testing LineString objects."
        prev = fromstr("POINT(0 0)")
        for l in self.geometries.linestrings:
            ls = fromstr(l.wkt)
            self.assertEqual(ls.geom_type, "LineString")
            self.assertEqual(ls.geom_typeid, 1)
            self.assertEqual(ls.empty, False)
            self.assertEqual(ls.ring, False)
            if hasattr(l, "centroid"):
                self.assertEqual(l.centroid, ls.centroid.tuple)
            if hasattr(l, "tup"):
                self.assertEqual(l.tup, ls.tuple)

            self.assertEqual(ls, fromstr(l.wkt))
            self.assertEqual(False, ls == prev)  # Use assertEqual to test __eq__
            self.assertRaises(GEOSIndexError, ls.__getitem__, len(ls))
            prev = ls

            # Creating a LineString from a tuple, list, and numpy array
            self.assertEqual(ls, LineString(ls.tuple))  # tuple
            self.assertEqual(ls, LineString(*ls.tuple))  # as individual arguments
            self.assertEqual(ls, LineString([list(tup) for tup in ls.tuple]))  # as list
            # Point individual arguments
            self.assertEqual(ls.wkt, LineString(*tuple(Point(tup) for tup in ls.tuple)).wkt)
            if numpy:
                self.assertEqual(ls, LineString(numpy.array(ls.tuple)))  # as numpy array
예제 #13
0
    def test_buffer(self):
        "Testing buffer()."
        for bg in self.geometries.buffer_geoms:
            g = fromstr(bg.wkt)

            # The buffer we expect
            exp_buf = fromstr(bg.buffer_wkt)
            quadsegs = bg.quadsegs
            width = bg.width

            # Can't use a floating-point for the number of quadsegs.
            self.assertRaises(ctypes.ArgumentError, g.buffer, width, float(quadsegs))

            # Constructing our buffer
            buf = g.buffer(width, quadsegs)
            self.assertEqual(exp_buf.num_coords, buf.num_coords)
            self.assertEqual(len(exp_buf), len(buf))

            # Now assuring that each point in the buffer is almost equal
            for j in range(len(exp_buf)):
                exp_ring = exp_buf[j]
                buf_ring = buf[j]
                self.assertEqual(len(exp_ring), len(buf_ring))
                for k in range(len(exp_ring)):
                    # Asserting the X, Y of each point are almost equal (due to floating point imprecision)
                    self.assertAlmostEqual(exp_ring[k][0], buf_ring[k][0], 9)
                    self.assertAlmostEqual(exp_ring[k][1], buf_ring[k][1], 9)
예제 #14
0
 def create_block_for_feature(self, feature, road):  
     """
     Creates a block from a given feature.
     """     
     from_addr_left = feature.get('L_F_ADD')
     to_addr_left = feature.get('L_T_ADD')
     from_addr_right = feature.get('R_F_ADD')
     to_addr_right = feature.get('R_T_ADD')
     
     try:
         linestring = feature.geom
     except OGRException:
         return
     
     try:
         block_number = self.get_block_number(
             from_addr_left, to_addr_left, from_addr_right, to_addr_right)
     except NoAddressesException:
         # For our purposes a block without addresses is no block at all
         log.debug('Skipping block creation for segment of %s with no addresses.' % road.full_name)
         return
     except InvalidAddressRangeException:
         # Don't die on these types of errors--will fix them as we can
         log.error('Invalid address range for segment of %s, bounds were %s, %s, %s, %s.' % 
             (road.full_name, from_addr_left, to_addr_left, from_addr_right, to_addr_right))
         return
         
     try:
         block = Block.objects.get(
             number=block_number,
             road=road
             )
             
         # Add new segment to set composing this block
         self.paths[(block_number, road)].append(linestring.geos)
         
         # Attempt to merge line set
         path = self.paths[(block_number, road)].merged
         
         # If the merge succeeded this will be LineString (otherwise MultiLineString)
         if type(path) == LineString:
             location = fromstr(self.estimate_point_along_linestring(path, 0.50), srid=9102671)
             block.location = location
             block.save()
         else:
             # Discontinous segments could not be merged, assume there are more to come.
             return
     except Block.DoesNotExist:
         # Get center-point of segment
         location = fromstr(self.estimate_point_along_linestring(linestring, 0.50), srid=9102671)
             
         Block.objects.create(
             number=block_number,
             road=road,
             location=location
             )
             
         # Store linestring in case this block turns out to have multiple segments
         self.paths[(block_number, road)] = MultiLineString(linestring.geos)
예제 #15
0
 def test_unary_union(self):
     "Testing unary_union."
     for i in range(len(self.geometries.topology_geoms)):
         a = fromstr(self.geometries.topology_geoms[i].wkt_a)
         b = fromstr(self.geometries.topology_geoms[i].wkt_b)
         u1 = fromstr(self.geometries.union_geoms[i].wkt)
         u2 = GeometryCollection(a, b).unary_union
         self.assertTrue(u1.equals(u2))
예제 #16
0
 def test_union(self):
     geom = Point(-95.363151, 29.763374, srid=4326)
     ptown = City.objects.annotate(union=functions.Union('point', geom)).get(name='Dallas')
     tol = 0.00001
     # Undefined ordering
     expected1 = fromstr('MULTIPOINT(-96.801611 32.782057,-95.363151 29.763374)', srid=4326)
     expected2 = fromstr('MULTIPOINT(-95.363151 29.763374,-96.801611 32.782057)', srid=4326)
     self.assertTrue(expected1.equals_exact(ptown.union, tol) or expected2.equals_exact(ptown.union, tol))
예제 #17
0
 def test_isvalid(self):
     valid_geom = fromstr('POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))')
     invalid_geom = fromstr('POLYGON((0 0, 0 1, 1 1, 1 0, 1 1, 1 0, 0 0))')
     State.objects.create(name='valid', poly=valid_geom)
     State.objects.create(name='invalid', poly=invalid_geom)
     valid = State.objects.filter(name='valid').annotate(isvalid=functions.IsValid('poly')).first()
     invalid = State.objects.filter(name='invalid').annotate(isvalid=functions.IsValid('poly')).first()
     self.assertIs(valid.isvalid, True)
     self.assertIs(invalid.isvalid, False)
예제 #18
0
 def handle(self, *args, **options):
     User.objects.all().delete()
     user = User.objects.create_user(
                username = '******',
                email = '*****@*****.**',
                password = '******',
            )
     user.first_name = 'test'
     user.last_name = 'commenter'
     user.set_password('test')
     lat = settings.DEFAULT_LATITUDE
     lng = settings.DEFAULT_LONGITUDE
     lString = 'POINT(%s %s)' % (lng, lat)
     new_position = Position(
         user=user,
         name=user.username,
         geometry=fromstr(lString))
     user.save()
     new_position.save()
     user.position = new_position
     user.save()
     new_position.user = user
     new_position.save()
     
     user2 = User.objects.create_user(username = '******',
                 email = '*****@*****.**',
                 password = '******',)
     user2.first_name = 'stella'
     user2.last_name = 'silverstein'
     user2.set_password('stella')
     new_position = Position(
                 user=user2,
                 name=user2.username,
                 geometry=fromstr(lString))
     user2.save()
     new_position.save()
     user2.position = new_position
     user2.save()
     new_position.user = user2
     new_position.save()
     
     superuser = User.objects.create_superuser(username = '******',
                 email = '*****@*****.**',
                 password = '******',)
     superuser.first_name = 'super'
     superuser.last_name = 'silverstein'
     superuser.set_password('super')
     new_position = Position(
                 user=superuser,
                 name=superuser.username,
                 geometry=fromstr(lString))
     superuser.save()
     new_position.save()
     superuser.position = new_position
     superuser.save()
     new_position.user = superuser
     new_position.save()
예제 #19
0
 def test_relate_pattern(self):
     "Testing relate() and relate_pattern()."
     g = fromstr("POINT (0 0)")
     self.assertRaises(GEOSException, g.relate_pattern, 0, "invalid pattern, yo")
     for rg in self.geometries.relate_geoms:
         a = fromstr(rg.wkt_a)
         b = fromstr(rg.wkt_b)
         self.assertEqual(rg.result, a.relate_pattern(b, rg.pattern))
         self.assertEqual(rg.pattern, a.relate(b))
예제 #20
0
파일: forms.py 프로젝트: VinceRafale/tiger
 def _post_clean(self):
     delivery_area = self.cleaned_data.get('delivery_area')
     if delivery_area is not None:
         try:
             fromstr(delivery_area)
         except GEOSException:
             self._update_errors({'delivery_area': 'FAIL'})
             return
     super(OrderSettingsForm, self)._post_clean()
예제 #21
0
    def test_mutable_geometries(self):
        "Testing the mutability of Polygons and Geometry Collections."
        # ### Testing the mutability of Polygons ###
        for p in self.geometries.polygons:
            poly = fromstr(p.wkt)

            # Should only be able to use __setitem__ with LinearRing geometries.
            self.assertRaises(TypeError, poly.__setitem__, 0, LineString((1, 1), (2, 2)))

            # Constructing the new shell by adding 500 to every point in the old shell.
            shell_tup = poly.shell.tuple
            new_coords = []
            for point in shell_tup:
                new_coords.append((point[0] + 500.0, point[1] + 500.0))
            new_shell = LinearRing(*tuple(new_coords))

            # Assigning polygon's exterior ring w/the new shell
            poly.exterior_ring = new_shell
            str(new_shell)  # new shell is still accessible
            self.assertEqual(poly.exterior_ring, new_shell)
            self.assertEqual(poly[0], new_shell)

        # ### Testing the mutability of Geometry Collections
        for tg in self.geometries.multipoints:
            mp = fromstr(tg.wkt)
            for i in range(len(mp)):
                # Creating a random point.
                pnt = mp[i]
                new = Point(random.randint(21, 100), random.randint(21, 100))
                # Testing the assignment
                mp[i] = new
                str(new)  # what was used for the assignment is still accessible
                self.assertEqual(mp[i], new)
                self.assertEqual(mp[i].wkt, new.wkt)
                self.assertNotEqual(pnt, mp[i])

        # MultiPolygons involve much more memory management because each
        # Polygon w/in the collection has its own rings.
        for tg in self.geometries.multipolygons:
            mpoly = fromstr(tg.wkt)
            for i in range(len(mpoly)):
                poly = mpoly[i]
                old_poly = mpoly[i]
                # Offsetting the each ring in the polygon by 500.
                for j in range(len(poly)):
                    r = poly[j]
                    for k in range(len(r)):
                        r[k] = (r[k][0] + 500.0, r[k][1] + 500.0)
                    poly[j] = r

                self.assertNotEqual(mpoly[i], poly)
                # Testing the assignment
                mpoly[i] = poly
                str(poly)  # Still accessible
                self.assertEqual(mpoly[i], poly)
                self.assertNotEqual(mpoly[i], old_poly)
예제 #22
0
 def test_line_merge(self):
     "Testing line merge support"
     ref_geoms = (fromstr('LINESTRING(1 1, 1 1, 3 3)'),
                  fromstr('MULTILINESTRING((1 1, 3 3), (3 3, 4 2))'),
                  )
     ref_merged = (fromstr('LINESTRING(1 1, 3 3)'),
                   fromstr('LINESTRING (1 1, 3 3, 4 2)'),
                   )
     for geom, merged in zip(ref_geoms, ref_merged):
         self.assertEqual(merged, geom.merged)
예제 #23
0
 def test_ewkt(self):
     "Testing EWKT."
     srids = (-1, 32140)
     for srid in srids:
         for p in self.geometries.polygons:
             ewkt = "SRID=%d;%s" % (srid, p.wkt)
             poly = fromstr(ewkt)
             self.assertEqual(srid, poly.srid)
             self.assertEqual(srid, poly.shell.srid)
             self.assertEqual(srid, fromstr(poly.ewkt).srid)  # Checking export
예제 #24
0
    def test_points(self):
        "Testing Point objects."
        prev = fromstr('POINT(0 0)')
        for p in self.geometries.points:
            # Creating the point from the WKT
            pnt = fromstr(p.wkt)
            self.assertEqual(pnt.geom_type, 'Point')
            self.assertEqual(pnt.geom_typeid, 0)
            self.assertEqual(pnt.dims, 0)
            self.assertEqual(p.x, pnt.x)
            self.assertEqual(p.y, pnt.y)
            self.assertEqual(pnt, fromstr(p.wkt))
            self.assertEqual(False, pnt == prev)  # Use assertEqual to test __eq__

            # Making sure that the point's X, Y components are what we expect
            self.assertAlmostEqual(p.x, pnt.tuple[0], 9)
            self.assertAlmostEqual(p.y, pnt.tuple[1], 9)

            # Testing the third dimension, and getting the tuple arguments
            if hasattr(p, 'z'):
                self.assertEqual(True, pnt.hasz)
                self.assertEqual(p.z, pnt.z)
                self.assertEqual(p.z, pnt.tuple[2], 9)
                tup_args = (p.x, p.y, p.z)
                set_tup1 = (2.71, 3.14, 5.23)
                set_tup2 = (5.23, 2.71, 3.14)
            else:
                self.assertEqual(False, pnt.hasz)
                self.assertIsNone(pnt.z)
                tup_args = (p.x, p.y)
                set_tup1 = (2.71, 3.14)
                set_tup2 = (3.14, 2.71)

            # Centroid operation on point should be point itself
            self.assertEqual(p.centroid, pnt.centroid.tuple)

            # Now testing the different constructors
            pnt2 = Point(tup_args)  # e.g., Point((1, 2))
            pnt3 = Point(*tup_args)  # e.g., Point(1, 2)
            self.assertEqual(pnt, pnt2)
            self.assertEqual(pnt, pnt3)

            # Now testing setting the x and y
            pnt.y = 3.14
            pnt.x = 2.71
            self.assertEqual(3.14, pnt.y)
            self.assertEqual(2.71, pnt.x)

            # Setting via the tuple/coords property
            pnt.tuple = set_tup1
            self.assertEqual(set_tup1, pnt.tuple)
            pnt.coords = set_tup2
            self.assertEqual(set_tup2, pnt.coords)

            prev = pnt  # setting the previous geometry
예제 #25
0
 def test_union(self):
     "Testing union()."
     for i in range(len(self.geometries.topology_geoms)):
         a = fromstr(self.geometries.topology_geoms[i].wkt_a)
         b = fromstr(self.geometries.topology_geoms[i].wkt_b)
         u1 = fromstr(self.geometries.union_geoms[i].wkt)
         u2 = a.union(b)
         self.assertEqual(u1, u2)
         self.assertEqual(u1, a | b)  # __or__ is union operator
         a |= b  # testing __ior__
         self.assertEqual(u1, a)
예제 #26
0
 def test_symdifference(self):
     "Testing sym_difference()."
     for i in range(len(self.geometries.topology_geoms)):
         a = fromstr(self.geometries.topology_geoms[i].wkt_a)
         b = fromstr(self.geometries.topology_geoms[i].wkt_b)
         d1 = fromstr(self.geometries.sdiff_geoms[i].wkt)
         d2 = a.sym_difference(b)
         self.assertEqual(d1, d2)
         self.assertEqual(d1, a ^ b)  # __xor__ is symmetric difference operator
         a ^= b  # testing __ixor__
         self.assertEqual(d1, a)
예제 #27
0
    def test_gdal(self):
        "Testing `ogr` and `srs` properties."
        g1 = fromstr('POINT(5 23)')
        self.assertEqual(True, isinstance(g1.ogr, gdal.OGRGeometry))
        self.assertEqual(g1.srs, None)

        g2 = fromstr('LINESTRING(0 0, 5 5, 23 23)', srid=4326)
        self.assertEqual(True, isinstance(g2.ogr, gdal.OGRGeometry))
        self.assertEqual(True, isinstance(g2.srs, gdal.SpatialReference))
        self.assertEqual(g2.hex, g2.ogr.hex)
        self.assertEqual('WGS 84', g2.srs.name)
예제 #28
0
 def test_difference(self):
     "Testing difference()."
     for i in range(len(self.geometries.topology_geoms)):
         a = fromstr(self.geometries.topology_geoms[i].wkt_a)
         b = fromstr(self.geometries.topology_geoms[i].wkt_b)
         d1 = fromstr(self.geometries.diff_geoms[i].wkt)
         d2 = a.difference(b)
         self.assertEqual(d1, d2)
         self.assertEqual(d1, a - b)  # __sub__ is difference operator
         a -= b  # testing __isub__
         self.assertEqual(d1, a)
예제 #29
0
 def test_intersection(self):
     "Testing intersects() and intersection()."
     for i in range(len(self.geometries.topology_geoms)):
         a = fromstr(self.geometries.topology_geoms[i].wkt_a)
         b = fromstr(self.geometries.topology_geoms[i].wkt_b)
         i1 = fromstr(self.geometries.intersect_geoms[i].wkt)
         self.assertEqual(True, a.intersects(b))
         i2 = a.intersection(b)
         self.assertEqual(i1, i2)
         self.assertEqual(i1, a & b)  # __and__ is intersection operator
         a &= b  # testing __iand__
         self.assertEqual(i1, a)
예제 #30
0
파일: sandiego.py 프로젝트: atogle/OTM2
def transform_geometry(geometry_wkt):
    """
    Some records in the SD database are stored with a different srid. Fix them.
    """
    geom = fromstr(geometry_wkt, srid=4326)
    try:
        geom.transform(SpatialReference(3857), clone=True)
        return geom
    except OGRException:
        # make sure 102646 is in the db:
        # http://spatialreference.org/ref/esri/102646/
        bad_geom = fromstr(geometry_wkt, srid=102646)
        return bad_geom.transform(SpatialReference(4326), clone=True)
예제 #31
0
        else:
            lon = request.GET.get("lon")
            lat = request.GET.get("lat")
            address = request.GET.get("q")
            if lat and lon:
                point_str = "POINT(%s %s)" % (lon, lat)
            elif address:
                addrs = []
                match_index = int(request.GET.get('index', -1))
                point_str = search.search_address(address, match_index, addrs)
            else:
                raise InputValidationException(
                    'Must supply either a `q`, `lat` `lon`, or a report `id`')

            radius = float(request.GET.get('r', 4))
            pnt = fromstr(point_str, srid=4326)
            reports = Report.objects.filter(
                is_confirmed=True, point__distance_lte=(pnt, D(
                    km=radius))).distance(pnt).order_by('distance')[:100]
            return (reports)

    def post(self, request):
        request.POST = MobileReportAPI._transform_params(request.POST)

        if not request.POST.has_key('device_id'):
            raise InputValidationException(
                'General Service Error: No device_id')

        if not MobileReportAPI._nonce_ok(request):
            raise InvalidAPIKey('Invalid API Key')
예제 #32
0
 def set_location_from_latlon(self, lat, lon):
     self.created_location = Location.objects.create(
         location=(fromstr("POINT(%s %s)" % (lon, lat))))
     self.save()
예제 #33
0
     'renamed_fields': {
         'tree': 'tree_id'
     },
     'removed_fields': {'title', 'reported', 'reported_by', 'comment'},
     'missing_fields': set()
 },
 'boundary': {
     'command_line_flag': '-b',
     'model_class': Boundary,
     'common_fields': {'name'},
     'renamed_fields': {
         'geometry': 'geom'
     },
     'removed_fields': {'region_id', 'city', 'state', 'county'},
     'value_transformers': {
         'geometry': (lambda x: fromstr(x, srid=4326)),
     },
 },
 'tree': {
     'command_line_flag': '-t',
     'model_class': Tree,
     'dependencies': {
         'species': 'species',
         'user': '******',
         'plot': 'plot'
     },
     'common_fields': {
         'readonly', 'canopy_height', 'date_planted', 'date_removed',
         'height'
     },
     'renamed_fields': {
예제 #34
0
 def get_location(latitude, longitude):
     from django.contrib.gis.geos import fromstr
     return fromstr("POINT({0} {1})".format(latitude, longitude), srid=4326)
예제 #35
0
파일: models.py 프로젝트: k34n4y138/geolist
 def save(self, *args, **kwargs):
     self.geom = fromstr(f'POINT({self.longitude} {self.latitude})',
                         srid=4326)
     return super(Place, self).save(*args, **kwargs)
예제 #36
0
def scresysed(request, criteria_id=None):
    initial = {}
    editmode = True if (criteria_id) else False
    if editmode:
        instance = SecureFeature.objects.get(id=criteria_id)

        # vuid to ogc_fid, scre_settvuid is saved as vuid but displayed as ogc_fid
        sett = AfgPplp.objects.get(vuid=instance.scre_settvuid)
        instance.scre_settvuid = sett.ogc_fid
    else:  # new entry mode
        instance = SecureFeature()
        initial['scre_username'] = request.user.username
        # initial['recstatus'] = 1 # default

    # current user is entry creator flag
    iscreator = False if editmode and request.user.username != instance.scre_username else True

    # has_delete_right flag
    has_delete_right = ('geodb.delete_afgincidentoasis'
                        in request.user.get_all_permissions())

    # disable fields flag
    readonly = True if not has_delete_right and editmode and not iscreator else False

    if request.POST:

        # pass values for form validation
        initial['scre_provid'] = request.POST['scre_provid']
        initial['scre_distid'] = request.POST['scre_distid']

        form = SecureFeatureForm(request.POST,
                                 instance=instance,
                                 initial=initial)

        # exclude username input field to prevent altering
        del form.fields['scre_username']

        # exclude recstatus field if common user
        if not has_delete_right:
            del form.fields['recstatus']

        if form.is_valid():

            # ogc_fid to vuid, do this after form validation to avoid error
            settp = AfgPplp.objects.get(ogc_fid=request.POST['scre_settvuid'])
            if (settp.vuid):
                vuid = settp.vuid
            else:
                pnt_wkt = 'POINT(' + str(settp.lon_x) + ' ' + str(
                    settp.lat_y) + ')'
                setta = AfgPpla.objects.get(wkb_geometry__contains=pnt_wkt)
                vuid = setta.vuid

            frm_incdtstr = request.POST.get('scre_incidentdatestr', '')
            frm_inctmstr = request.POST.get('scre_incidenttimestr', '')
            frm_lat = request.POST.get('scre_latitude', '0')
            frm_lon = request.POST.get('scre_longitude', '0')

            obj = form.save(
                commit=False)  # use this to assign values to excluded fields
            obj.scre_incidentdate = frm_incdtstr + " " + frm_inctmstr
            obj.mpoint = fromstr('Point(' + frm_lon + ' ' + frm_lat +
                                 ')') if frm_lon and frm_lat else None
            obj.scre_settvuid = vuid
            if obj.id:  # edit existing
                obj.userud = request.user.id
                obj.updatedatetime = datetime.datetime.today()
                if not has_delete_right:  # for regular user edit will set recstatus to 1
                    obj.recstatus = 1
            else:  # is new entry
                obj.scre_username = request.user.username
                obj.userid = request.user.id
                obj.entrydatetime = datetime.datetime.today()
            if obj.recstatus is None:
                obj.recstatus = 1

            form.save()

            return HttpResponseRedirect('/securitydb/list/')
        else:
            print '\n', 'form not valid'
            formerrors = dict(form.errors.items())
            for key in formerrors:
                print '%s: %s' % (key, formerrors[key].as_text())
    else:
        form = SecureFeatureForm(instance=instance, initial=initial)

    #  exclude recstatus field if not has_delete_right
    if not has_delete_right:  # is common user
        if 'recstatus' in form.fields:
            del form.fields['recstatus']

    # disable input fields if readonly
    if (readonly):
        for key, field in form.fields.iteritems():
            field.widget.attrs['disabled'] = 'disabled'

    return render(request, 'editform.html', {
        'form': form,
        'readonly': readonly
    })
예제 #37
0
    def handle(self, **options):
        commit = options.get('commit')
        region = options.get('region')
        excel_file = options.get('excel_file')
        risk_analysis = options.get('risk_analysis')
        excel_metadata_file = options.get('excel_metadata_file')
        risk_app = options.get('risk_app')
        app = RiskApp.objects.get(name=risk_app)

        if region is None:
            raise CommandError(
                "Input Destination Region '--region' is mandatory")

        if risk_analysis is None:
            raise CommandError(
                "Input Risk Analysis associated to the File '--risk_analysis' is mandatory"
            )

        if not excel_file or len(excel_file) == 0:
            raise CommandError(
                "Input Risk Data Table '--excel_file' is mandatory")

        risk = RiskAnalysis.objects.get(name=risk_analysis, app=app)

        wb = xlrd.open_workbook(filename=excel_file)
        region = Region.objects.get(name=region)
        region_code = region.administrative_divisions.order_by(
            'level'
        )[0].code  #region.administrative_divisions.filter(parent=None)[0].code

        scenarios = RiskAnalysisDymensionInfoAssociation.objects.filter(
            riskanalysis=risk, axis='x')
        round_periods = RiskAnalysisDymensionInfoAssociation.objects.filter(
            riskanalysis=risk, axis='y')

        # print('typename = %s' % (risk.layer.typename))

        table_name = risk.layer.typename.split(":")[1] \
            if ":" in risk.layer.typename else risk.layer.typename

        for scenario in scenarios:
            # Dump Vectorial Data from DB
            datastore = settings.OGC_SERVER['default']['DATASTORE']
            if (datastore):
                ogc_db_name = settings.DATABASES[datastore]['NAME']
                ogc_db_user = settings.DATABASES[datastore]['USER']
                ogc_db_passwd = settings.DATABASES[datastore]['PASSWORD']
                ogc_db_host = settings.DATABASES[datastore]['HOST']
                ogc_db_port = settings.DATABASES[datastore]['PORT']

            sheet = wb.sheet_by_name(scenario.value)
            print 'reading from sheet {}'.format(scenario.value)
            row_headers = sheet.row(0)
            for rp_idx, rp in enumerate(round_periods):
                col_num = -1
                if app.name == RiskApp.APP_DATA_EXTRACTION:
                    for idx, cell_obj in enumerate(row_headers):
                        # cell_type_str = ctype_text.get(cell_obj.ctype, 'unknown type')
                        # print('(%s) %s %s' % (idx, cell_type_str, cell_obj.value))
                        try:
                            # if int(cell_obj.value) == int(rp.value):
                            # print('{} =? {}'.format(rp.value, cell_obj.value))
                            if self.to_int_if_number(
                                    str(cell_obj.value).strip()
                            ) == self.to_int_if_number(str(rp.value).strip()):
                                # print('[%s] (%s) RP-%s' % (scenario.value, idx, rp.value))
                                col_num = idx
                                break
                        except:
                            traceback.print_exc()
                            pass
                elif app.name == RiskApp.APP_COST_BENEFIT:
                    col_num = 0

                if col_num >= 0:
                    conn = self.get_db_conn(ogc_db_name, ogc_db_user,
                                            ogc_db_port, ogc_db_host,
                                            ogc_db_passwd)
                    try:
                        if app.name == RiskApp.APP_DATA_EXTRACTION:
                            for row_num in range(1, sheet.nrows):
                                cell_obj = sheet.cell(row_num, 5)
                                iso_country = str(
                                    sheet.cell(row_num, 2).value)[:2]
                                cell_type_str = ctype_text.get(
                                    cell_obj.ctype, 'unknown type')
                                value = sheet.cell(
                                    row_num, col_num).value if self.is_number(
                                        sheet.cell(row_num,
                                                   col_num).value) else None
                                # print('(%s) %s %s' % (idx, cell_type_str, cell_obj.value))
                                if cell_obj.value and value is not None:
                                    adm_code = cell_obj.value \
                                        if cell_type_str == 'text' \
                                        else iso_country + '{:05d}'.format(int(cell_obj.value))
                                    #print('adm code read from cell: {}'.format(adm_code))

                                    #check if exists ADM unit with given code and if ADM unit belongs to given region
                                    try:
                                        adm_div = AdministrativeDivision.objects.get(
                                            code=adm_code)
                                        region_match = adm_div.regions.get(
                                            name=region.name)
                                    except AdministrativeDivision.DoesNotExist:
                                        traceback.print_exc()
                                        pass

                                    parent_adm_div = None
                                    if adm_div.parent is not None:
                                        try:
                                            parent_adm_div = AdministrativeDivision.objects.get(
                                                id=adm_div.parent.id)
                                        except AdministrativeDivision.DoesNotExist:
                                            traceback.print_exc()
                                            pass
                                    #print('parent_adm_div = {}'.format(parent_adm_div.name))

                                    #print('[%s] (%s) %s (%s) / %s' % (scenario.value, rp.value, adm_div.name.encode('utf-8'), adm_code, value))
                                    #print('[%s] (%s) (%s) / %s' % (scenario.value, rp.value, adm_code, value))

                                    db_values = {
                                        'table':
                                        table_name,  # From rp.layer
                                        'the_geom':
                                        geos.fromstr(adm_div.geom,
                                                     srid=adm_div.srid),
                                        'dim1':
                                        scenario.value,
                                        'dim1_order':
                                        scenario.order,
                                        'dim2':
                                        rp.value,
                                        'dim2_order':
                                        rp.order,
                                        'dim3':
                                        None,
                                        'dim4':
                                        None,
                                        'dim5':
                                        None,
                                        'risk_analysis_id':
                                        risk.id,
                                        'risk_analysis':
                                        risk_analysis,
                                        'hazard_type':
                                        risk.hazard_type.mnemonic,
                                        'adm_name':
                                        adm_div.name.encode('utf-8').replace(
                                            "'", "''"),
                                        'adm_code':
                                        adm_div.code,
                                        'region':
                                        region.name,
                                        'adm_level':
                                        adm_div.level,
                                        'parent_adm_code':
                                        '' if parent_adm_div is None else
                                        parent_adm_div.code,
                                        'value':
                                        value
                                    }
                                    self.insert_db(conn, db_values, rp_idx)
                                    '''risk_adm = RiskAnalysisAdministrativeDivisionAssociation.\
                                        objects.\
                                        filter(riskanalysis=risk, administrativedivision=adm_div)
                                    if len(risk_adm) == 0:
                                        RiskAnalysisAdministrativeDivisionAssociation.\
                                            objects.\
                                            create(riskanalysis=risk, administrativedivision=adm_div)'''
                                    risk_adm, created = RiskAnalysisAdministrativeDivisionAssociation.objects.get_or_create(
                                        riskanalysis=risk,
                                        administrativedivision=adm_div)

                        elif app.name == RiskApp.APP_COST_BENEFIT:
                            cell_obj = sheet.cell(rp_idx + 1, 0)
                            cell_type_str = ctype_text.get(
                                cell_obj.ctype, 'unknown type')
                            if cell_obj.value:
                                adm_div = AdministrativeDivision.objects.get(
                                    name=region)
                                value = sheet.cell_value(rp_idx + 1, 1)
                                #print('[%s] (%s) %s / %s' % (scenario.value, rp.value, adm_div.name, value))

                                db_values = {
                                    'table':
                                    table_name,  # From rp.layer
                                    'the_geom':
                                    geos.fromstr(adm_div.geom,
                                                 srid=adm_div.srid),
                                    'dim1':
                                    scenario.value,
                                    'dim1_order':
                                    scenario.order,
                                    'dim2':
                                    rp.value,
                                    'dim2_order':
                                    rp.order,
                                    'dim3':
                                    None,
                                    'dim4':
                                    None,
                                    'dim5':
                                    None,
                                    'risk_analysis':
                                    risk_analysis,
                                    'hazard_type':
                                    risk.hazard_type.mnemonic,
                                    'adm_name':
                                    adm_div.name.encode('utf-8').replace(
                                        "'", "''"),
                                    'adm_code':
                                    adm_div.code,
                                    'region':
                                    region.name,
                                    'value':
                                    value
                                }
                                self.insert_db(conn, db_values, rp_idx)
                                risk_adm = RiskAnalysisAdministrativeDivisionAssociation.\
                                objects.\
                                filter(riskanalysis=risk, administrativedivision=adm_div)
                                if len(risk_adm) == 0:
                                    RiskAnalysisAdministrativeDivisionAssociation.\
                                    objects.\
                                    create(riskanalysis=risk, administrativedivision=adm_div)

                        # Finished Import: Commit on DB
                        conn.commit()
                    except Exception:
                        try:
                            conn.rollback()
                        except:
                            pass
                        raise CommandError(e)
                        #traceback.print_exc()
                    finally:
                        conn.close()

        # Import or Update Metadata if Metadata File has been specified/found
        if excel_metadata_file:
            call_command('importriskmetadata',
                         region=region.name,
                         excel_file=excel_metadata_file,
                         risk_analysis=risk_analysis,
                         risk_app=[app.name])
            risk.metadata_file = excel_metadata_file

        # Finalize
        risk.data_file = excel_file
        risk.region = region
        if commit:
            risk.save()

        return risk_analysis
예제 #38
0
def nearby(request):
    details = False
    lat = 0.0
    long = 0.0
    dist = ""
    near = []
    dirn = []
    count = 0

    if request.method == 'POST' and details == False:
        details = True
        lat = request.POST.get('lat')
        long = request.POST.get('long')
        location = fromstr(f'POINT({long} {lat})', srid=4326)
        dist = request.POST.get('dist')

        m = folium.Map(location=[lat, long], zoom_start=12)

        folium.Marker([lat, long],
                      popup='',
                      tooltip='Your Location!',
                      icon=folium.Icon(color='red')).add_to(m),

        if dist == "lt5":
            near = School.objects.annotate(
                distance=Distance("location", location) /
                1000).filter(distance__lte=5).order_by("distance")
        elif dist == "lt1":
            near = School.objects.annotate(
                distance=Distance("location", location) /
                1000).filter(distance__lte=1).order_by("distance")
        elif dist == "lt10":
            near = School.objects.annotate(
                distance=Distance("location", location) /
                1000).filter(distance__lte=10).order_by("distance")
        else:
            near = School.objects.annotate(
                distance=Distance("location", location) /
                1000).filter(distance__lte=15).order_by("distance")
        #print(near)

        if len(near) > 0:
            for it in near:
                org = it.name.replace(' ', '+')
                org_address = it.address.replace(' ', '+')
                link = "https://www.google.com/maps/dir/?api=1&destination=" + org + "+" + org_address
                dirn.append(link)
                tooltip = "Name:" + it.name
                folium.Marker([it.location.y, it.location.x],
                              popup='<a href="' + link + '">GetDirection</a>',
                              tooltip=tooltip).add_to(m)

        m.save('templates/schools/map_near_school.html')

    return render(
        request, 'schools/nearby_schools.html', {
            'near': near,
            'details': details,
            'dist': dist,
            'dirn': dirn,
            'zi': zip(near, dirn)
        })
예제 #39
0
def location_from_coords(lng, lat):
    return fromstr('POINT(%.5f %.5f)' % (float(lng), float(lat)))
예제 #40
0
 def test_wkb(self):
     "Testing WKB output."
     for g in self.geometries.hex_wkt:
         geom = fromstr(g.wkt)
         wkb = geom.wkb
         self.assertEqual(b2a_hex(wkb).decode().upper(), g.hex)
예제 #41
0
 def test_wkt(self):
     "Testing WKT output."
     for g in self.geometries.wkt_out:
         geom = fromstr(g.wkt)
         if geom.hasz:
             self.assertEqual(g.ewkt, geom.wkt)
예제 #42
0
def ll_to_point(longitude, latitude, srid=4326):
    return fromstr('POINT(%s %s)' % (longitude, latitude), srid=4326)
예제 #43
0
    def test_snap_to_grid(self):
        # Let's try and break snap_to_grid() with bad combinations of arguments.
        for bad_args in ((), range(3), range(5)):
            with self.assertRaises(ValueError):
                Country.objects.annotate(snap=functions.SnapToGrid('mpoly', *bad_args))
        for bad_args in (('1.0',), (1.0, None), tuple(map(six.text_type, range(4)))):
            with self.assertRaises(TypeError):
                Country.objects.annotate(snap=functions.SnapToGrid('mpoly', *bad_args))

        # Boundary for San Marino, courtesy of Bjorn Sandvik of thematicmapping.org
        # from the world borders dataset he provides.
        wkt = ('MULTIPOLYGON(((12.41580 43.95795,12.45055 43.97972,12.45389 43.98167,'
               '12.46250 43.98472,12.47167 43.98694,12.49278 43.98917,'
               '12.50555 43.98861,12.51000 43.98694,12.51028 43.98277,'
               '12.51167 43.94333,12.51056 43.93916,12.49639 43.92333,'
               '12.49500 43.91472,12.48778 43.90583,12.47444 43.89722,'
               '12.46472 43.89555,12.45917 43.89611,12.41639 43.90472,'
               '12.41222 43.90610,12.40782 43.91366,12.40389 43.92667,'
               '12.40500 43.94833,12.40889 43.95499,12.41580 43.95795)))')
        Country.objects.create(name='San Marino', mpoly=fromstr(wkt))

        # Because floating-point arithmetic isn't exact, we set a tolerance
        # to pass into GEOS `equals_exact`.
        tol = 0.000000001

        # SELECT AsText(ST_SnapToGrid("geoapp_country"."mpoly", 0.1)) FROM "geoapp_country"
        # WHERE "geoapp_country"."name" = 'San Marino';
        ref = fromstr('MULTIPOLYGON(((12.4 44,12.5 44,12.5 43.9,12.4 43.9,12.4 44)))')
        self.assertTrue(
            ref.equals_exact(
                Country.objects.annotate(
                    snap=functions.SnapToGrid('mpoly', 0.1)
                ).get(name='San Marino').snap,
                tol
            )
        )

        # SELECT AsText(ST_SnapToGrid("geoapp_country"."mpoly", 0.05, 0.23)) FROM "geoapp_country"
        # WHERE "geoapp_country"."name" = 'San Marino';
        ref = fromstr('MULTIPOLYGON(((12.4 43.93,12.45 43.93,12.5 43.93,12.45 43.93,12.4 43.93)))')
        self.assertTrue(
            ref.equals_exact(
                Country.objects.annotate(
                    snap=functions.SnapToGrid('mpoly', 0.05, 0.23)
                ).get(name='San Marino').snap,
                tol
            )
        )

        # SELECT AsText(ST_SnapToGrid("geoapp_country"."mpoly", 0.5, 0.17, 0.05, 0.23)) FROM "geoapp_country"
        # WHERE "geoapp_country"."name" = 'San Marino';
        ref = fromstr(
            'MULTIPOLYGON(((12.4 43.87,12.45 43.87,12.45 44.1,12.5 44.1,12.5 43.87,12.45 43.87,12.4 43.87)))'
        )
        self.assertTrue(
            ref.equals_exact(
                Country.objects.annotate(
                    snap=functions.SnapToGrid('mpoly', 0.05, 0.23, 0.5, 0.17)
                ).get(name='San Marino').snap,
                tol
            )
        )
예제 #44
0
    def prepare_documents_for_search_index(self):
        """
        Generates a list of specialized resource based documents to support resource search

        """
        
        document = Entity()
        document.property = self.property
        document.entitytypeid = self.entitytypeid
        document.entityid = self.entityid
        document.value = self.value
        document.label = self.label
        document.businesstablename = self.businesstablename
        document.primaryname = self.get_primary_name()
        document.child_entities = []
        document.dates = []
        document.extendeddates = []
        document.domains = []
        document.geometries = []
        document.numbers = []

        for entity in self.flatten():
            if entity.entityid != self.entityid:
                if entity.businesstablename == 'domains':
                    value = archesmodels.Values.objects.get(pk=entity.value)
                    entity_copy = entity.copy()
                    entity_copy.conceptid = value.conceptid_id
                    document.domains.append(entity_copy)
                elif entity.businesstablename == 'dates':
                    document.dates.append(entity)
                    document.extendeddates.append(entity)
                elif entity.businesstablename == 'numbers':
                    document.numbers.append(entity)
                elif entity.businesstablename == 'geometries':
                    entity.value = JSONDeserializer().deserialize(fromstr(entity.value).json)
                    document.geometries.append(entity)
                else:
                    document.child_entities.append(entity)

                if entity.entitytypeid in settings.EXTENDED_DATE_NODES:
                    document.extendeddates.append(entity)

        doc = JSONSerializer().serializeToPython(document)

        # documents = super(Resource, self).prepare_documents_for_search_index()
        # for doc in documents:
        
        ## index dates to extended date mapping
        for entity in doc['extendeddates']:
            date = date_to_int(entity['value'])
            entity['value'] = date
        
        ## index dates groups to extended date groups mapping
        doc['extendeddategroups'] = []
        for branch,labels in settings.INDEXED_DATE_BRANCH_FORMATIONS.iteritems():
            for nodes in self.get_nodes(branch,keys=['value']):
                doc['extendeddategroups'].append({
                    'value': date_to_int(nodes[labels[0]]),
                    'conceptid': nodes[labels[1]]
                })
    
        return [doc]
def standard_deviation_ellipses(geoqueryset,
                                point_attribute_name='point',
                                num_of_std=1,
                                fix_points=True):
    """
	Accepts a GeoQuerySet and generates one or more standard deviation ellipses
	demonstrating the geospatial distribution of where its points occur.
	
	Returns a one-to-many list of the ellipses as Polygon objects. 
	
	The standard deviation ellipse illustrates the average variation in 
	the distance of points from the mean center, as well as their direction.
	
	By default, the function expects the Point field on your model to be called 'point'.
	
	If the point field is called something else, change the kwarg 'point_attribute_name'
	to whatever your field might be called.
	
	Also by default, the function will nudge slightly apart any identical points and 
	only return the first standard deviation ellipse. If you'd like to change that behavior,
	change the corresponding kwargs.
	
	h3. Example usage
	
		>> import calculate
		>> calculate.standard_deviation_ellipses(qs)
		[<Polygon object at 0x77a1c34>]
	
	h3. Dependencies
	
		* "django":http://www.djangoproject.com/
		* "geodjango":http://www.geodjango.org/
		* "psql ellipse() function":http://postgis.refractions.net/support/wiki/index.php?plpgsqlfunctions
	
	h3. Documentation

		* "standard deviation ellipse":http://www.spatialanalysisonline.com/output/html/Directionalanalysisofpointdatasets.html
		* "This code is translated from SQL by Francis Dupont":http://postgis.refractions.net/pipermail/postgis-users/2008-June/020354.html
		
	"""
    if not isinstance(geoqueryset, GeoQuerySet):
        raise TypeError(
            'First parameter must be a Django GeoQuerySet. You submitted a %s object'
            % type(geoqueryset))

    n = len(geoqueryset)

    if n < 3:
        return [None]

    if fix_points:
        calculate.nudge_points(geoqueryset,
                               point_attribute_name=point_attribute_name)

    avg_x = calculate.mean(
        [abs(getattr(p, point_attribute_name).x) for p in geoqueryset])
    avg_y = calculate.mean(
        [abs(getattr(p, point_attribute_name).y) for p in geoqueryset])
    center_x = calculate.mean(
        [getattr(p, point_attribute_name).x for p in geoqueryset])
    center_y = calculate.mean(
        [getattr(p, point_attribute_name).y for p in geoqueryset])

    sum_square_diff_avg_x = sum([
        math.pow((abs(getattr(p, point_attribute_name).x) - avg_x), 2)
        for p in geoqueryset
    ])
    sum_square_diff_avg_y = sum([
        math.pow((abs(getattr(p, point_attribute_name).y) - avg_y), 2)
        for p in geoqueryset
    ])
    sum_diff_avg_x_y = sum([(abs(getattr(p, point_attribute_name).x) - avg_x) *
                            (abs(getattr(p, point_attribute_name).y) - avg_y)
                            for p in geoqueryset])
    sum_square_diff_avg_x_y = sum([
        math.pow((abs(getattr(p, point_attribute_name).x) - avg_x) *
                 (abs(getattr(p, point_attribute_name).y) - avg_y), 2)
        for p in geoqueryset
    ])
    constant = math.sqrt(
        math.pow((sum_square_diff_avg_x - sum_square_diff_avg_y), 2) +
        (4 * sum_square_diff_avg_x_y))
    theta = math.atan(
        (sum_square_diff_avg_x - sum_square_diff_avg_y + constant) /
        (2 * sum_diff_avg_x_y))

    stdx_sum_x_y_cos_sin_theta = sum([
        math.pow((((getattr(p, point_attribute_name).x - center_x) *
                   math.cos(theta)) -
                  ((getattr(p, point_attribute_name).y - center_y) *
                   math.sin(theta))), 2) for p in geoqueryset
    ])
    stdy_sum_x_y_sin_cos_theta = sum([
        math.pow((((getattr(p, point_attribute_name).x - center_x) *
                   math.sin(theta)) -
                  ((getattr(p, point_attribute_name).y - center_y) *
                   math.cos(theta))), 2) for p in geoqueryset
    ])

    stdx = math.sqrt((2 * stdx_sum_x_y_cos_sin_theta) / (n - 2))
    stdy = math.sqrt((2 * stdy_sum_x_y_sin_cos_theta) / (n - 2))

    results = []
    from django.db import connection
    cursor = connection.cursor()
    while num_of_std:
        cursor.execute(
            """SELECT ellipse(%s, %s, (%s * %s), (%s * %s), %s, 40);""" %
            (center_x, center_y, num_of_std, stdx, num_of_std, stdy, theta))
        results.append(fromstr(cursor.fetchall()[0][0], srid=4326))
        num_of_std -= 1
    return results
예제 #46
0
    def test_relate_lookup(self):
        "Testing the 'relate' lookup type."
        # To make things more interesting, we will have our Texas reference point in
        # different SRIDs.
        pnt1 = fromstr("POINT (649287.0363174 4177429.4494686)", srid=2847)
        pnt2 = fromstr("POINT(-98.4919715741052 29.4333344025053)", srid=4326)

        # Not passing in a geometry as first param raises a TypeError when
        # initializing the QuerySet.
        with self.assertRaises(ValueError):
            Country.objects.filter(mpoly__relate=(23, "foo"))

        # Making sure the right exception is raised for the given
        # bad arguments.
        for bad_args, e in [
            ((pnt1, 0), ValueError),
            ((pnt2, "T*T***FF*", 0), ValueError),
        ]:
            qs = Country.objects.filter(mpoly__relate=bad_args)
            with self.assertRaises(e):
                qs.count()

        contains_mask = "T*T***FF*"
        within_mask = "T*F**F***"
        intersects_mask = "T********"
        # Relate works differently on Oracle.
        if connection.ops.oracle:
            contains_mask = "contains"
            within_mask = "inside"
            # TODO: This is not quite the same as the PostGIS mask above
            intersects_mask = "overlapbdyintersect"

        # Testing contains relation mask.
        if connection.features.supports_transform:
            self.assertEqual(
                Country.objects.get(mpoly__relate=(pnt1, contains_mask)).name,
                "Texas",
            )
        self.assertEqual(
            "Texas",
            Country.objects.get(mpoly__relate=(pnt2, contains_mask)).name)

        # Testing within relation mask.
        ks = State.objects.get(name="Kansas")
        self.assertEqual(
            "Lawrence",
            City.objects.get(point__relate=(ks.poly, within_mask)).name)

        # Testing intersection relation mask.
        if not connection.ops.oracle:
            if connection.features.supports_transform:
                self.assertEqual(
                    Country.objects.get(mpoly__relate=(pnt1,
                                                       intersects_mask)).name,
                    "Texas",
                )
            self.assertEqual(
                "Texas",
                Country.objects.get(mpoly__relate=(pnt2,
                                                   intersects_mask)).name)
            self.assertEqual(
                "Lawrence",
                City.objects.get(point__relate=(ks.poly,
                                                intersects_mask)).name,
            )

        # With a complex geometry expression
        mask = "anyinteract" if connection.ops.oracle else within_mask
        self.assertFalse(
            City.objects.exclude(
                point__relate=(functions.Union("point", "point"), mask)))
예제 #47
0
 def _get_point(self, data):
     longitude, latitude = data.split(',')
     return fromstr(f'POINT({longitude} {latitude})')
예제 #48
0
    def test_polygons(self):
        "Testing Polygon objects."

        prev = fromstr('POINT(0 0)')
        for p in self.geometries.polygons:
            # Creating the Polygon, testing its properties.
            poly = fromstr(p.wkt)
            self.assertEqual(poly.geom_type, 'Polygon')
            self.assertEqual(poly.geom_typeid, 3)
            self.assertEqual(poly.empty, False)
            self.assertEqual(poly.ring, False)
            self.assertEqual(p.n_i, poly.num_interior_rings)
            self.assertEqual(p.n_i + 1, len(poly))  # Testing __len__
            self.assertEqual(p.n_p, poly.num_points)

            # Area & Centroid
            self.assertAlmostEqual(p.area, poly.area, 9)
            self.assertAlmostEqual(p.centroid[0], poly.centroid.tuple[0], 9)
            self.assertAlmostEqual(p.centroid[1], poly.centroid.tuple[1], 9)

            # Testing the geometry equivalence
            self.assertEqual(True, poly == fromstr(p.wkt))
            self.assertEqual(
                False,
                poly == prev)  # Should not be equal to previous geometry
            self.assertEqual(True, poly != prev)

            # Testing the exterior ring
            ring = poly.exterior_ring
            self.assertEqual(ring.geom_type, 'LinearRing')
            self.assertEqual(ring.geom_typeid, 2)
            if p.ext_ring_cs:
                self.assertEqual(p.ext_ring_cs, ring.tuple)
                self.assertEqual(p.ext_ring_cs,
                                 poly[0].tuple)  # Testing __getitem__

            # Testing __getitem__ and __setitem__ on invalid indices
            self.assertRaises(GEOSIndexError, poly.__getitem__, len(poly))
            self.assertRaises(GEOSIndexError, poly.__setitem__, len(poly),
                              False)
            self.assertRaises(GEOSIndexError, poly.__getitem__,
                              -1 * len(poly) - 1)

            # Testing __iter__
            for r in poly:
                self.assertEqual(r.geom_type, 'LinearRing')
                self.assertEqual(r.geom_typeid, 2)

            # Testing polygon construction.
            self.assertRaises(TypeError, Polygon, 0, [1, 2, 3])
            self.assertRaises(TypeError, Polygon, 'foo')

            # Polygon(shell, (hole1, ... holeN))
            rings = tuple(r for r in poly)
            self.assertEqual(poly, Polygon(rings[0], rings[1:]))

            # Polygon(shell_tuple, hole_tuple1, ... , hole_tupleN)
            ring_tuples = tuple(r.tuple for r in poly)
            self.assertEqual(poly, Polygon(*ring_tuples))

            # Constructing with tuples of LinearRings.
            self.assertEqual(poly.wkt, Polygon(*tuple(r for r in poly)).wkt)
            self.assertEqual(
                poly.wkt,
                Polygon(*tuple(LinearRing(r.tuple) for r in poly)).wkt)
예제 #49
0
    def test_relate_lookup(self):
        "Testing the 'relate' lookup type."
        # To make things more interesting, we will have our Texas reference point in
        # different SRIDs.
        pnt1 = fromstr('POINT (649287.0363174 4177429.4494686)', srid=2847)
        pnt2 = fromstr('POINT(-98.4919715741052 29.4333344025053)', srid=4326)

        # Not passing in a geometry as first param raises a TypeError when
        # initializing the QuerySet.
        with self.assertRaises(ValueError):
            Country.objects.filter(mpoly__relate=(23, 'foo'))

        # Making sure the right exception is raised for the given
        # bad arguments.
        for bad_args, e in [((pnt1, 0), ValueError),
                            ((pnt2, 'T*T***FF*', 0), ValueError)]:
            qs = Country.objects.filter(mpoly__relate=bad_args)
            with self.assertRaises(e):
                qs.count()

        # Relate works differently for the different backends.
        if postgis or spatialite:
            contains_mask = 'T*T***FF*'
            within_mask = 'T*F**F***'
            intersects_mask = 'T********'
        elif oracle:
            contains_mask = 'contains'
            within_mask = 'inside'
            # TODO: This is not quite the same as the PostGIS mask above
            intersects_mask = 'overlapbdyintersect'

        # Testing contains relation mask.
        self.assertEqual(
            'Texas',
            Country.objects.get(mpoly__relate=(pnt1, contains_mask)).name)
        self.assertEqual(
            'Texas',
            Country.objects.get(mpoly__relate=(pnt2, contains_mask)).name)

        # Testing within relation mask.
        ks = State.objects.get(name='Kansas')
        self.assertEqual(
            'Lawrence',
            City.objects.get(point__relate=(ks.poly, within_mask)).name)

        # Testing intersection relation mask.
        if not oracle:
            self.assertEqual(
                'Texas',
                Country.objects.get(mpoly__relate=(pnt1,
                                                   intersects_mask)).name)
            self.assertEqual(
                'Texas',
                Country.objects.get(mpoly__relate=(pnt2,
                                                   intersects_mask)).name)
            self.assertEqual(
                'Lawrence',
                City.objects.get(point__relate=(ks.poly,
                                                intersects_mask)).name)

        # With a complex geometry expression
        mask = 'anyinteract' if oracle else within_mask
        self.assertFalse(
            City.objects.exclude(
                point__relate=(functions.Union('point', 'point'), mask)))
예제 #50
0
 def test_hex(self):
     "Testing HEX output."
     for g in self.geometries.hex_wkt:
         geom = fromstr(g.wkt)
         self.assertEqual(g.hex, geom.hex.decode())
예제 #51
0
 def create_cmd(line):    
     geopoint = fromstr("POINT(%s %s)" % (float(check_field(line, 'shape_pt_lat')), float(check_field(line, 'shape_pt_lon'))))
     return Shape.objects.get_or_create(shape_id=check_field(line, 'shape_id'), 
                             geopoint=geopoint,
                             pt_sequence=check_field(line, 'shape_pt_sequence'))
예제 #52
0
 def multigeometry(self):
     # Returns a multipolygon of the whole study region
     mgeom = geos.fromstr('MULTIPOLYGON EMPTY')
     for sr in self.iterator():
         mgeom.append(sr.geometry)
     return mgeom
예제 #53
0
            for feat in layer:
                print('processing feature {}'.format(count))
                geom = geos.fromstr(feat.geom.wkt, srid=4326)                                                    
                if tolerance > 0:                    
                    geom = geom.simplify(tolerance, preserve_topology=True)

                # Generalize to 'Multiploygon'                
                if isinstance(geom, geos.Polygon):
                    geom = geos.MultiPolygon(geom)
                
                if count == 0:
                    polygon_union = geom.buffer(0)
                else:
                    polygon_union = polygon_union.union(geom.buffer(0))
                count += 1
    return geos.fromstr(polygon_union.wkt, srid=4326)

def import_events(data_from_feed, tolerance = 0.0001):
    """
    For every shapefile in _merged folder, creates or updates event in Django DB and Geoserver DB. 
    The geometry inserted in Geoserver DB is the union of all features found in layer.
    """
    db = DbUtils()
    conn = db.get_db_conn()
    curs = conn.cursor()
    allowed_extensions = [".shp"]

    try:
        for root, dirs, files in os.walk(SHAPEFILES_BASE_DIR):
            print('shapefiles base dir: {}'.format(SHAPEFILES_BASE_DIR))            
            for d in dirs:
예제 #54
0
def import_events(data_from_feed, tolerance = 0.0001):
    """
    For every shapefile in _merged folder, creates or updates event in Django DB and Geoserver DB. 
    The geometry inserted in Geoserver DB is the union of all features found in layer.
    """
    db = DbUtils()
    conn = db.get_db_conn()
    curs = conn.cursor()
    allowed_extensions = [".shp"]

    try:
        for root, dirs, files in os.walk(SHAPEFILES_BASE_DIR):
            print('shapefiles base dir: {}'.format(SHAPEFILES_BASE_DIR))            
            for d in dirs:
                if d.endswith('_merged'):  

                    print('found merged folder')                     
                    for f in os.listdir(os.path.join(SHAPEFILES_BASE_DIR, d)):                        
                        if f.endswith(tuple(allowed_extensions)):
                            print('found shapefiles: {}'.format(f))

                            '''try:
                                filepath = os.path.join(SHAPEFILES_BASE_DIR, d, f)
                                ds = DataSource(filepath)
                            except GDALException, e:
                                traceback.print_exc()
                                break
                            for layer in ds:
                                count = 0 
                                polygon_union = None   
                                for feat in layer:   
                                    print('processing feature {}'.format(count))                                 
                                    # Simplify the Geometry
                                    geom = geos.fromstr(feat.geom.wkt, srid=4326)                                                    
                                    if tolerance > 0:                    
                                        geom = geom.simplify(tolerance, preserve_topology=True)

                                    # Generalize to 'Multiploygon'                
                                    if isinstance(geom, geos.Polygon):
                                        geom = geos.MultiPolygon(geom)
                                    
                                    if count == 0:
                                        polygon_union = geom.buffer(0)
                                    else:
                                        polygon_union = polygon_union.union(geom.buffer(0))
                                    count += 1                                
                                try:
                                    print('re-extracting geom from union polygon')
                                    polygon_union = geos.fromstr(polygon_union.wkt, srid=4326)
                                except:
                                    break'''                                                        
                            ems = d.split('_')[0]
                            select_template = "SELECT ST_AsText(ST_Union(ARRAY(SELECT ST_Buffer(the_geom, 1e-5) FROM {})))".format(ems)
                            row = None
                            try:
                                curs.execute(select_template)
                                row = curs.fetchone()
                            except:
                                pass   
                            #set default multipolygon
                            ext_coords = ((0, 0), (0, 1), (1, 1), (1, 0), (0, 0))  
                            int_coords = ((0.4, 0.4), (0.4, 0.6), (0.6, 0.6), (0.6, 0.4), (0.4, 0.4))                       
                            polygon_union = geos.MultiPolygon(geos.Polygon(ext_coords, int_coords), srid=4326)
                            if row:
                                polygon_union = geos.fromstr(row[0], srid=4326)
                            
                            # Update event in Django
                            event_from_feed = get_event_from_feed(data_from_feed, ems)
                            new_event = generate_event_from_feed(event_from_feed, polygon_union)
                            
                            if new_event:
                                params = { 
                                    'geom': polygon_union,
                                    'event_id': new_event.id,
                                    'begin_date': new_event.begin_date,
                                    'end_date': new_event.end_date
                                }
                                update_template = """INSERT INTO events (the_geom, event_id, begin_date, end_date) 
                                                        SELECT 
                                                            '{geom}', '{event_id}', '{begin_date}', '{end_date}'
                                                        ON CONFLICT (event_id)
                                                        DO UPDATE SET the_geom = excluded.the_geom;"""
                                print update_template.format(**params)
                                curs.execute(update_template.format(**params))
                    archive_path = os.path.join(SHAPEFILES_BASE_DIR, d, 'archive')
                    if not os.path.exists(archive_path):
                        os.makedirs(archive_path)
                    for f2 in os.listdir(os.path.join(SHAPEFILES_BASE_DIR, d)):
                        filepath = os.path.join(SHAPEFILES_BASE_DIR, d, f2) 
                        if os.path.isfile(filepath):
                            print('moving file {} to {}'.format(f2, archive_path))
                            os.rename(filepath, os.path.join(archive_path, f2))

        conn.commit()        
    except Exception:
        try:
            conn.rollback()
        except:
            pass

        traceback.print_exc()
    finally:
        conn.close()
예제 #55
0
 def convex_hull_multigeometry(self):
     mgeom = geos.fromstr('MULTIPOLYGON EMPTY').convex_hull
     for sr in self.iterator():
         mgeom.append(sr.geometry)
     return mgeom
예제 #56
0
 def test_wkt(self):
     "Testing WKT output."
     for g in self.geometries.wkt_out:
         geom = fromstr(g.wkt)
         if geom.hasz and geos_version_info()['version'] >= '3.3.0':
             self.assertEqual(g.ewkt, geom.wkt)
예제 #57
0
def import_from_shape(upload,
                      start_row=0,
                      max_rows=200000,
                      create_int_style_cols=True):
    """
  a shapeUpload object
  max_rows - any more than this is ignored
  centroid - if it's a (multi)polygon, should we also create a geometry_centroid field
  """

    upload.status = 2  #set this right away so it doesn't get reprocessed
    upload.save()
    ds = DataSource(upload.shapefile)
    layer = ds[0]
    fields = layer.fields

    num_features = len(layer)
    #set max # of _style features
    max_distinct_style_vals = max(min(num_features / 100, 50), 10)
    print 'there are %d features' % num_features
    upload.total_rows = num_features
    if not num_features:
        print 'no rows, returning'
        upload.status = 6
        upload.save()
        return

    rows = []
    #get field types
    field_map = {
        'OFTString': 'STRING',
        'OFTReal': 'NUMBER',
        'OFTInteger': 'NUMBER',
        'OFTDate': 'DATETIME'
    }
    field_types = [field_map[f.__name__] for f in layer.field_types]
    field_layers = layer.fields

    #insert geometry layers first
    field_layers.insert(0, 'geometry')
    field_types.insert(0, 'LOCATION')
    field_layers.insert(1, 'geometry_vertex_count')
    field_types.insert(1, 'NUMBER')

    if upload.create_simplify:
        field_layers.insert(0, 'geometry_simplified')
        field_types.insert(0, 'LOCATION')
        field_layers.insert(1, 'geometry_simplified_vertex_count')
        field_types.insert(1, 'NUMBER')

    #use sorted dict so we can ensure table has geom columns upfront
    field_dict = SortedDict(zip(field_layers, field_types))

    #set up extra fields if creating int/style cols
    if create_int_style_cols:
        int_style_dict = {}
        for field, field_type in field_dict.items():
            if field_type == 'STRING':
                field_dict[field + '_ft_style'] = 'NUMBER'
                int_style_dict[field] = {}
        print field_dict

    #add some custom import fields
    field_dict['import_notes'] = 'STRING'

    print 'FIELD DICT', field_dict
    print 'starting to process'
    for i, feat in enumerate(layer):
        if i > max_rows:
            continue
        if start_row and i < start_row:
            continue
        upload.rows_processed = i + 1
        if not i % ((num_features / 50) or 5):
            print upload.rows_processed, 'rp'
            upload.save()
        upload.save()
        rd = {}
        #geom = fromstr(feat.geom.wkt,srid=srid)
        if layer.srs:
            try:
                geom = OGRGeometry(feat.geom.wkt, layer.srs.proj4)
                geom.transform(4326)
            except Exception, e:
                print 'FAIL GEOM'
                print e,
                geom = None
        else:
            geom = OGRGeometry(feat.geom.wkt)

        if geom:
            geom = fromstr(geom.wkt)
            #create optional centroid for polys
            if upload.create_centroid and 'oly' in geom.geom_type:
                field_dict['geometry_pos'] = 'LOCATION'
                rd['geometry_pos'] = geom.point_on_surface.kml

            if upload.create_centroid_poly and 'oly' in geom.geom_type:
                field_dict['geometry_pos_poly_2'] = 'LOCATION'
                field_dict['geometry_pos_poly_3'] = 'LOCATION'

                rd['geometry_pos_poly_2'] = geom.point_on_surface.buffer(
                    .0001, 10).kml
                rd['geometry_pos_poly_3'] = geom.point_on_surface.buffer(
                    .0005, 10).kml

            #if it's > 1M characters, we need to simplify it for FT
            simplify_tolerance = .0001
            while len(geom.kml) > 1000000:
                geom = geom.simplify(simplify_tolerance)
                print 'simplified to %f' % simplify_tolerance
                rd['import_notes'] = 'simplified to %d DD' % simplify_tolerance
                simplify_tolerance = simplify_tolerance * 1.5

            if not geom.valid:
                rd['import_notes'] = '<br>Geometry not valid'

            kml = geom.kml
            rd['geometry'] = kml
            rd['geometry_vertex_count'] = geom.num_coords

            if upload.create_simplify and not 'oint' in geom.geom_type:
                amt = .002
                if 'oly' in geom.geom_type:
                    buffer_geom = geom.buffer(amt)
                    buffer_geom = buffer_geom.buffer(amt * -1)
                    simple_geom = buffer_geom.simplify(amt)
                else:
                    simple_geom = geom.simplify(amt)

                rd['geometry_simplified'] = simple_geom.kml
                rd['geometry_simplified_vertex_count'] = simple_geom.num_coords

        for f in fields:
            val = feat.get(f)
            #make sure we have proper null type for diff fields
            if val == '<Null>':
                continue
            if not val:
                continue

            if field_dict[f] == 'DATETIME':
                val = val.isoformat().split('T')[0]

            if field_dict[f] == 'STRING' \
              and create_int_style_cols \
              and field_dict.has_key(f + '_ft_style'):

                #check to see if we have a number for this yet
                try:
                    rd[f + '_ft_style'] = int_style_dict[f][val]
                except:
                    int_style_dict[f][val] = len(int_style_dict[f])
                    rd[f + '_ft_style'] = int_style_dict[f][val]
                #however if we have too many distinct vals, let's just not do this anymore
                if len(int_style_dict[f]) > max_distinct_style_vals:
                    print 'DELETING FD %s' % f
                    del field_dict[f + '_ft_style']
                    del rd[f + '_ft_style']
                    #sucks, but now we should just remove all these fields from previous rows
                    for srow in rows:
                        try:
                            del srow[f + '_ft_style']
                        except:
                            pass  #probably this was a null value?

            rd[f] = val
        rows.append(rd)
        #let's process 10k rows at a time.. not keep everything in memory
        if len(rows) > 10000:
            uploadRows(upload, field_dict, rows)
            rows = []
예제 #58
0
def calcElev(linestring):
    # Calculate area in image to get
    # Get bounding box of area
    bbox = linestring.extent  # GeoDjango extent

    # Holds coordinates where we check for elevation
    pointArrayX = []
    pointArrayY = []
    # Holds distance according to above coordinates
    distArray = []

    # Uglyness: guess the right UTM-Zone
    centerpt = geos.Point((bbox[0] + bbox[2]) / 2.0, (bbox[1] + bbox[3]) / 2.0,
                          srid=db_srid)
    centerpt.transform(4326)
    if centerpt.y > 0:
        localProjection = 32600
    else:
        localProjection = 32700
    localProjection = localProjection + int((centerpt.x + 180) / 6)
    linestring.transform(localProjection)

    #
    # Set distance for interpolation on line according to length of route
    # - projectedLinestrings.length defines length of route in meter
    # - stepDist defines how often we want to extract a height value
    #   i.e. stepDist=50 defines that we should extract an elevation value for
    #   every 50 meter along the route
    stepDist = 0
    if linestring.length < 2000:
        stepDist = 20
    elif linestring.length > 1999 and linestring.length < 4000:
        stepDist = 100
    elif linestring.length > 3999 and linestring.length < 10000:
        stepDist = 100
    else:
        stepDist = 200

    #
    # Make interpolation point along line with stepDist form above.
    # Add these point to arrays.
    # Need to convert line to Shapely since we are using interpolate function
    #
    wktLine = linestring.wkt
    shapelyLinestring = wkt.loads(wktLine)
    step = 0
    while step < linestring.length + stepDist:
        shapelyPoint = shapelyLinestring.interpolate(step)
        wktPoint = wkt.dumps(shapelyPoint)
        geoDjangoPoint = geos.fromstr(wktPoint, srid=localProjection)
        geoDjangoPoint.transform(db_srid)
        pointArrayX.append(geoDjangoPoint.x)
        pointArrayY.append(geoDjangoPoint.y)
        distArray.append(step)
        step = step + stepDist

    # Expand the bounding box with 200 meter on each side
    band_array, xmax, ymin, xmin, ymax = createRasterArray(
        bbox[0], bbox[3], bbox[2], bbox[1])

    ny, nx = band_array.shape

    # Turn these into arrays of x & y coords
    xi = np.array(pointArrayX, dtype=np.float)
    yi = np.array(pointArrayY, dtype=np.float)

    # Now, we'll set points outside the boundaries to lie along an edge
    xi[xi > xmax] = xmax
    xi[xi < xmin] = xmin
    yi[yi > ymax] = ymax
    yi[yi < ymin] = ymin

    # We need to convert these to (float) indicies
    #   (xi should range from 0 to (nx - 1), etc)
    xi = (nx - 1) * (xi - xmin) / (xmax - xmin)
    yi = -(ny - 1) * (yi - ymax) / (ymax - ymin)

    # Interpolate elevation values
    # map_coordinates does cubic interpolation by default,
    # use "order=1" to preform bilinear interpolation
    elev = map_coordinates(band_array, [yi, xi], order=1)

    return (distArray, elev, pointArrayX, pointArrayY)
예제 #59
0
 def test_make_valid(self):
     invalid_geom = fromstr('POLYGON((0 0, 0 1, 1 1, 1 0, 1 1, 1 0, 0 0))')
     State.objects.create(name='invalid', poly=invalid_geom)
     invalid = State.objects.filter(name='invalid').annotate(repaired=functions.MakeValid('poly')).first()
     self.assertEqual(invalid.repaired.valid, True)
     self.assertEqual(invalid.repaired, fromstr('POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))'))
예제 #60
0
 def test_isvalid_lookup(self):
     invalid_geom = fromstr('POLYGON((0 0, 0 1, 1 1, 1 0, 1 1, 1 0, 0 0))')
     State.objects.create(name='invalid', poly=invalid_geom)
     self.assertEqual(State.objects.filter(poly__isvalid=False).count(), 1)
     self.assertEqual(State.objects.filter(poly__isvalid=True).count(), State.objects.count() - 1)