1 from django.test import TestCase
2 from django.test.client import Client
3 from django.conf import settings
4 from django.conf.urls.defaults import *
5 from django.contrib.gis.geos import *
6 from madrona.studyregion.models import StudyRegion
7 from django.core import serializers
8 from manipulators import *
9 from madrona.features.models import Feature, PointFeature, LineFeature, PolygonFeature, FeatureCollection
10 from madrona.features.forms import FeatureForm
11 from madrona.features import register
12 from django.contrib.auth.models import User
13 from django.test.utils import override_settings
14 import json
15 import re
29
30 - class Options(BaseManipulator.Options):
34
35 manipulatorsDict[TestManipulator.Options.name] = TestManipulator
38 fixtures = ['manipulators_test_data']
39
41 '''
42 Build geometries for the following test cases:
43 code0_poly clips successfully
44 code1_poly overlaps with estuary, oceanic part chosen
45 code2_poly target geometry lies outside of geometry being clipped against
46 or in the case of Estuary clipping, no estuaries were found to clip against
47 code3_poly target geometry is not valid
48 code4_poly this code has the most meanings, each depending on context
49 in the case of Estuary clipping, it means the target was estuary only
50 code5_poly overlaps with estuary, estuary part chosen
51 other case one or more required kwargs not provided
52 '''
53
54 self.code0_poly = fromstr('SRID=4326;POLYGON ((-1 -1, 1 -1, 1 -3, -1 -3, -1 -1))')
55 self.code1_poly = fromstr('SRID=4326;POLYGON ((-1 1, 1 1, 1 -1, -1 -1, -1 1))')
56 self.code2_poly = fromstr('SRID=4326;POLYGON ((3 3, 4 3, 4 4, 3 4, 3 3))')
57 self.code3_poly = fromstr('SRID=4326;POLYGON ((3 3, 4 3, 3 4, 4 4, 3 3))')
58 self.code4_poly = fromstr('SRID=4326;POLYGON ((0 2, 1 0, 2 2, 0 2))')
59 self.code5_poly = fromstr('SRID=4326;POLYGON ((0 2, 2 2, 2 1, 0 1, 0 2))')
60
61 self.study_region = fromstr('SRID=4326;POLYGON ((-2 2, 2 2, 2 -2, -2 -2, -2 2))')
62
63 self.est1 = fromstr('SRID=4326;POLYGON ((0 2, 1 0, 2 2, 0 2))')
64 self.est2 = fromstr('SRID=4326;POLYGON ((0 2, -1 0, -2 2, 0 2))')
65 self.ests = MultiPolygon(self.est1, self.est2)
66
67 self.client = Client()
68
70
71 self.code0_poly = None
72 self.code1_poly = None
73 self.code2_poly = None
74 self.code3_poly = None
75 self.code4_poly = None
76 self.code5_poly = None
77
78 self.study_region = None
79
80 self.est1 = None
81 self.est2 = None
82 self.ests = None
83
84 self.client = None
85
86 @override_settings(GEOMETRY_CLIENT_SRID=4326)
88 '''
89 Tests the following:
90 clip to graticule
91 '''
92
93 response1 = self.client.post('/manipulators/ClipToGraticule/', {'target_shape': display_kml(self.code1_poly), 'west': .5, 'east': -.5})
94 self.assertEquals(response1.status_code, 200)
95 graticule_clipper = ClipToGraticuleManipulator(target_shape=display_kml(self.code1_poly), west=.5, east=-.5)
96 result = graticule_clipper.manipulate()
97 gclip = result['clipped_shape']
98 self.assertAlmostEquals(result["clipped_shape"].area, 2, places=1)
99
100 @override_settings(GEOMETRY_CLIENT_SRID=4326)
102 '''
103 Tests the following:
104 clipped to study region
105 outside study region
106 geometry not valid
107 '''
108
109 response0 = self.client.post('/manipulators/ClipToStudyRegion/', {'target_shape': display_kml(self.code0_poly), 'study_region': self.study_region.wkt})
110 self.assertEquals(response0.status_code, 200)
111 studyregion_clipper = ClipToStudyRegionManipulator(target_shape=display_kml(self.code0_poly), study_region=self.study_region)
112 result = studyregion_clipper.manipulate()
113 self.assertAlmostEquals(result["clipped_shape"].area, 2, places=1)
114
115
116 response2 = self.client.post('/manipulators/ClipToStudyRegion/', {'target_shape': display_kml(self.code2_poly), 'study_region': self.study_region.wkt})
117 self.assertEquals(response2.status_code, 200)
118 try:
119 graticule_clipper = ClipToStudyRegionManipulator(target_shape=display_kml(self.code2_poly))
120 except HaltManipulations:
121 pass
122
123
124 response3 = self.client.post('/manipulators/ClipToStudyRegion/', {'target_shape': display_kml(self.code3_poly), 'study_region': self.study_region.wkt})
125 self.assertEquals(response3.status_code, 200)
126 try:
127 graticule_clipper = ClipToStudyRegionManipulator(target_shape=display_kml(self.code3_poly))
128 except InvalidGeometryException:
129 pass
130
132 '''
133 Tests the following:
134 clip to study region and clip to estuaries manipulations
135 clip to study region and clip to graticules manipulations
136
137 '''
138
139 response1 = self.client.post('/manipulators/ClipToStudyRegion,ClipToEstuaries/', {'target_shape': display_kml(self.code1_poly)})
140 self.assertEquals(response1.status_code, 200)
141
142 response1 = self.client.post('/manipulators/ClipToStudyRegion,ClipToGraticule/', {'target_shape': display_kml(self.code1_poly), 'east': .5})
143 self.assertEquals(response1.status_code, 200)
144
146 '''
147 Tests the following:
148 clipped to study region
149 '''
150 study_region = StudyRegion.objects.current().geometry
151
152 w = study_region.extent[0]
153 s = study_region.extent[1]
154 e = study_region.extent[2]
155 n = study_region.extent[3]
156
157 center_lat = study_region.centroid.y
158 center_lon = study_region.centroid.x
159
160 target_shape = Polygon(LinearRing([Point(center_lon, center_lat),
161 Point(e, center_lat),
162 Point(e, s),
163 Point(center_lon, s),
164 Point(center_lon, center_lat)]))
165 target_shape.set_srid(settings.GEOMETRY_DB_SRID)
166 target_shape.transform(settings.GEOMETRY_CLIENT_SRID)
167
168
169 response0 = self.client.post('/manipulators/ClipToStudyRegion/', {'target_shape': display_kml(target_shape)})
170 self.assertEquals(response0.status_code, 200)
171
172 @register
173 -class TestPoly(PolygonFeature):
174 type = models.CharField(max_length=1)
175
177 verbose_name = 'Test Poly'
178 form = 'madrona.manipulators.tests.TestPolyForm'
179 manipulators = ['madrona.manipulators.manipulators.ClipToStudyRegionManipulator']
183
193
197
198 @register
199 -class TestLine(LineFeature):
210
213 incident = models.CharField(max_length=1)
214
216 verbose_name = 'TestPoint'
217 form = 'madrona.manipulators.tests.TestPointForm'
218 manipulators = ['madrona.manipulators.manipulators.ClipToStudyRegionManipulator']
222
224 fixtures = ['example_data']
225
227 self.client = Client()
228 self.user = User.objects.create_user(
229 'featuretest', 'featuretest@madrona.org', password='pword')
230 self.client.login(username='featuretest', password='pword')
231
239
247
258
270
278
286
300
302
303 g = GEOSGeometry('SRID=4326;POLYGON((-120.234 34.46, -120.152 34.454, -120.162 34.547, -120.234 34.46))')
304 g.transform(settings.GEOMETRY_DB_SRID)
305
306
307 response = self.client.post('/manipulators//', {'target_shape': display_kml(g)})
308 self.assertEqual(response.status_code, 200, response.content)
309 kml = json.loads(response.content)['final_shape_kml']
310 search = re.search('<coordinates>(.*)</coordinates>', kml)
311 coords = search.groups()[0]
312 coords_count = coords.count(',') / 2
313 self.assertEqual(coords_count, g.num_coords, coords)
314
315
316 response = self.client.post('/manipulators/ClipToStudyRegion/', {'target_shape': display_kml(g)})
317 self.assertEqual(response.status_code, 200, response.content)
318 kml = json.loads(response.content)['final_shape_kml']
319 search = re.search('<coordinates>(.*)</coordinates>', kml)
320 coords = search.groups()[0]
321 coords_count = coords.count(',') / 2
322 self.assertGreater(coords_count, g.num_coords)
323