1 from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, HttpResponseBadRequest, HttpResponseServerError, HttpResponseForbidden
2 from django.template import RequestContext
3 from django.shortcuts import get_object_or_404, render_to_response
4 from madrona.common import default_mimetypes as mimetypes
5 from django.template.loader import render_to_string
6
7 from madrona.manipulators.manipulators import *
8 from django.db import models
9
10 from django.contrib.gis.geos import *
11 from madrona.studyregion.models import StudyRegion
12 from django.conf import settings
13
14 from django.contrib.contenttypes.models import ContentType
15 from django.utils import simplejson
16 from madrona.common.utils import clean_geometry
17
18
20 '''
21 Handler for AJAX mpa manipulators request
22 '''
23 try:
24 model = ContentType.objects.get(app_label=app_name, model=model_name)
25 manipulators = model.model_class().Options.manipulators
26 except Exception, e:
27 return HttpResponse("The following error was reported: '" + e.message + "', while generating manipulator list from application: " + app_name + " and model: " + model_name, status=404)
28
29 manip_text = [(manipulator.Options.name) for manipulator in manipulators]
30
31 return HttpResponse(simplejson.dumps(manip_text))
32
34 '''
35 multi_generic_manipulator_view takes a request and a list of manipulators
36 and runs the manipulators in the order provided
37 '''
38
39 kwargs = {}
40 for key,val in request.POST.items():
41 kwargs[str(key)] = str(val)
42 try:
43 submitted = kwargs['target_shape']
44 except:
45 return HttpResponse("Target shape not provided", status=500)
46
47 if manipulators:
48 manipulator_list = manipulators.split(',')
49 else:
50 manipulator_list = ['NullManipulator']
51
52 html_response = ''
53
54
55 for manipulator in manipulator_list:
56
57 manipClass = manipulatorsDict.get(manipulator)
58 if not manipClass:
59 return HttpResponse("Manipulator " + manipulator + " does not exist.", status=404)
60
61 try:
62
63 if request.method == 'GET':
64 if manipClass.Form.available:
65 form = manipClass.Form()
66 return render_to_response('common/base_form.html', RequestContext(request,{'form': form}))
67 else:
68 return HttpResponse("Manipulator " + manipulator + " does not support GET requests.", status=501)
69
70 else:
71 if manipClass.Form.available:
72 form = manipClass.Form(kwargs)
73 if form.is_valid():
74 initial_result = form.manipulation
75 else:
76 return HttpResponse(simplejson.dumps({"message": "form is not valid (missing arguments?)", "html": render_to_string('common/base_form.html', {'form': form}, RequestContext(request))}))
77 else:
78 manip_inst = manipClass(**kwargs)
79 initial_result = manip_inst.manipulate()
80
81 result = ensure_keys(initial_result)
82 new_shape = result['clipped_shape']
83
84
85 kwargs['target_shape'] = new_shape.wkt
86 html_response = html_response + result["html"]
87
88 except manipClass.InvalidGeometryException, e:
89 return respond_with_template(e.html, submitted, None, e.success)
90 except manipClass.InternalException, e:
91 return respond_with_template(e.html, submitted, None, e.success)
92 except manipClass.HaltManipulations, e:
93 return respond_with_template(e.html, submitted, None, e.success)
94 except Exception as e:
95 return respond_with_error(message=str(e))
96
97
98
99 new_shape.transform(settings.GEOMETRY_DB_SRID)
100 new_shape.transform(settings.GEOMETRY_CLIENT_SRID)
101 new_shape = clean_geometry(new_shape)
102
103
104
105
106
107 return respond_with_template(html_response, submitted, new_shape, result["success"])
108
110 if final_shape:
111 final_shape_kml = display_kml(final_shape)
112 else:
113 final_shape_kml = ''
114 user_shape = parsekml(submitted)
115 user_shape.srid = settings.GEOMETRY_CLIENT_SRID
116 user_shape.transform(settings.GEOMETRY_DB_SRID)
117
118 return HttpResponse(simplejson.dumps({"html": status_html, "submitted": submitted, "user_shape": user_shape.wkt, "final_shape_kml": final_shape_kml, "success": success}))
119
123
125 values.setdefault("html", "")
126 values.setdefault("clipped_shape", None)
127 values.setdefault("success", "1")
128 return values
129
131 trans_geom = StudyRegion.objects.current().geometry
132
133 w = trans_geom.extent[0]
134 s = trans_geom.extent[1]
135 e = trans_geom.extent[2]
136 n = trans_geom.extent[3]
137
138 center_lat = trans_geom.centroid.y
139 center_lon = trans_geom.centroid.x
140
141 target_shape = Polygon(LinearRing([Point(center_lon, center_lat),
142 Point(e, center_lat),
143 Point(e, s),
144 Point(center_lon, s),
145 Point(center_lon, center_lat)]))
146
147 target_shape.set_srid(settings.GEOMETRY_DB_SRID)
148 target_shape.transform(settings.GEOMETRY_CLIENT_SRID)
149
150 new_req = HttpRequest()
151 new_req.method = 'POST'
152 new_req.POST.update({'target_shape':target_shape.wkt, "north":"40", "south":"20", "east":"-117", "west":"-118"})
153 response = multi_generic_manipulator_view(new_req, 'ClipToStudyRegion,ClipToGraticule')
154
155 return response
156