Skip to content

Commit

Permalink
Merge pull request #115 from meomancer/development
Browse files Browse the repository at this point in the history
clipping with native resolution
  • Loading branch information
sbsimo authored Oct 27, 2017
2 parents 379e368 + 50780f5 commit 776e52b
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion django_project/clip-and-ship/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import urllib2

import subprocess
from pyproj import Proj, transform
from django.conf import settings
from django.http import Http404, HttpResponse, JsonResponse
from django.shortcuts import render
Expand All @@ -18,6 +19,11 @@
'format=geotiff&crs=EPSG:4326&bbox={bbox}&width={width}&' \
'height={height}'

WCS_URL_2_0_1 = settings.GEOSERVER_BASE_URL + \
'wcs?request=getcoverage&' \
'version=2.0.1&service=WCS&coverageid={layer_name}&' \
'format=geotiff&crs=EPSG:4326&subset=E({x1},{x2})&subset=N({y1},{y2})'

MAX_CLIP_SIZE = settings.MAXIMUM_CLIP_SIZE
TILE_SAMPLE_SIZE = 100 # tile sample size will be 100*100

Expand Down Expand Up @@ -69,6 +75,28 @@ def download_wcs(layername, bbox_string, width, height, raster_filepath):
fh.close()


def download_wcs_v2(layername, x1, x2, y1, y2, raster_filepath):
""" Download clipped image from wcs.
:param raster_filepath: filepath for downloaded wcs clipped
:type raster_filepath: str
:return:
"""
wcs_formatted_url = WCS_URL_2_0_1.format(
layer_name=layername,
x1=x1,
x2=x2,
y1=y1,
y2=y2
)

response = urllib2.urlopen(wcs_formatted_url)
fh = open(raster_filepath, "w")
fh.write(response.read())
fh.close()


def clip_layer(request, layername):
"""Clipping raster layer and save to temp folder.
Clipping layer by bbox or by geojson.
Expand Down Expand Up @@ -186,8 +214,15 @@ def clip_layer(request, layername):
raster_filepath = os.path.join(
temporary_folder,
layer.title + '.' + extention)
x1, x2 = bbox_array[0], bbox_array[2]
y1, y2 = bbox_array[1], bbox_array[3]
inProj = Proj(init='epsg:4326')
outProj = Proj(init='epsg:32618')

x1, y1 = transform(inProj, outProj, x1, y1)
x2, y2 = transform(inProj, outProj, x2, y2)

download_wcs(layername, bbox_string, width, height, raster_filepath)
download_wcs_v2(layername, x1, x2, y1, y2, raster_filepath)

if not geojson:
response = JsonResponse({
Expand Down

0 comments on commit 776e52b

Please sign in to comment.