Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions django_project/core/base_static/js/jquery.form.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions django_project/core/settings/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
'js/Leaflet.MakiMarkers.js',
'js/jquery.plugin.js',
'js/jquery.datepick.js',
'js/jquery.form.min.js',
'js/map.js'
),
'output_filename': 'js/contrib.js',
Expand Down
32 changes: 32 additions & 0 deletions django_project/imagery_requests/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
logger = logging.getLogger(__name__)

import json

import django.forms as forms

from .models import ImageryRequest, RequestDate
Expand Down Expand Up @@ -75,3 +77,33 @@ class RequestDateForm(forms.ModelForm):
class Meta:
model = RequestDate
fields = ['date', 'time']


class GeoJsonForm(forms.Form):
geojson = forms.FileField(error_messages={'required': 'No file selected.'})

def clean_geojson(self):
try:
data = json.load(self.cleaned_data['geojson'])
except ValueError:
raise forms.ValidationError('Data was not JSON serializeable.')

if not isinstance(data, dict):
raise forms.ValidationError('Data was not a JSON object.')

if 'geometry' not in data:
message = 'The "geometry" member is required and was not found.'
raise forms.ValidationError(message)

if 'type' not in data['geometry']:
message = 'The "type" member is required and was not found.'
raise forms.ValidationError(message)

if 'coordinates' not in data['geometry']:
message = 'The "coordinates" member is required and was not found.'
raise forms.ValidationError(message)

if data['geometry']['type'] != 'Polygon':
raise forms.ValidationError('Geometry type has to be "Polygon".')

return data
19 changes: 19 additions & 0 deletions django_project/imagery_requests/templates/geojson_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
<div class="field" style="margin:0px;">
{{ form.geojson }}

<div class="mini ui buttons" style="margin-bottom:5px;">
<div id="submit_geojson" class="ui positive button">Import</div>
<div class="or"></div>
<div id="cancel_geojson" class="ui button">Cancel</div>
</div>
</div>

{% if form.geojson.errors %}
{% for error in form.geojson.errors %}
<div class="ui pointing red label">
{{ error }}
</div>
{% endfor %}
{% endif %}
</form>
58 changes: 56 additions & 2 deletions django_project/imagery_requests/templates/request_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
</div>
<div class="field">
<label>Imagery request area of interest:</label>
<div id="area_import_div">
<label style="font-size:12px;font-style:italic;">
Draw polygon on map or
<a href="">import geojson</a>
</label>
</div>
<div id="map" class="map" style="height: 450px"></div>
{% if form.area_of_interest.errors %}
{% for error in form.area_of_interest.errors %}
Expand Down Expand Up @@ -158,7 +164,6 @@
drawnItems.clearLayers();

drawnItems.addLayer(layer);

// dump to textarea
$('#id_area_of_interest').val(JSON.stringify(layer.toGeoJSON()['geometry']));
});
Expand All @@ -181,7 +186,56 @@
});
});

//map.fitBounds(drawnItems.getBounds());
// show form for geojson import
$('#area_import_div').on('click', 'a', function(e) {
e.preventDefault();
var form = $('#date_form');

$.ajax({
method: 'GET',
url: "/requests/import-geojson/",
}).done(function(data) {
$('#area_import_div').html(data)
})

});

// submit imported geojson
$('#area_import_div').on('click', '#submit_geojson', function() {

$('#area_import_div form').ajaxSubmit({
type: 'POST',
url: "/requests/import-geojson/",
contentType: "application/json; charset=utf-8",
success: function(response) {
$('#area_import_div').html(
'<label style="font-size:12px;font-style:italic;">' +
'Draw polygon on map or <a href="">import geojson</a>');

var geojsonFeature = jQuery.parseJSON(response);
var layer = L.geoJson(geojsonFeature);

// delete previous layer
drawnItems.clearLayers();

drawnItems.addLayer(layer);

// dump to textarea
$('#id_area_of_interest').val(response);
},
error: function(response) {
$('#area_import_div').html(response.responseText)
}
})
});

// cancel geojson import
$('#area_import_div').on('click', '#cancel_geojson', function() {
$('#area_import_div').html(
'<label style="font-size:12px;font-style:italic;">' +
'Draw polygon on map or <a href="">import geojson</a>');
});


</script>
{% endblock %}
5 changes: 4 additions & 1 deletion django_project/imagery_requests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
ViewRequest,
ListRequests,
DownloadRequest,
ImportGeoJson
)

urlpatterns = patterns(
Expand Down Expand Up @@ -41,5 +42,7 @@
name='view_request'
),
url(r'^requests/download/(?P<pk>\d+)/$', DownloadRequest.as_view(),
name='download_request')
name='download_request'),
url(r'^requests/import-geojson/$', ImportGeoJson.as_view(),
name='import_geojson')
)
Loading