|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +############################################################################### |
| 5 | +# Copyright Kitware Inc. and Epidemico Inc. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 ( the "License" ); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +############################################################################### |
| 19 | +import os |
| 20 | +from urllib import urlretrieve |
| 21 | +import zipfile |
| 22 | + |
| 23 | +import gdal |
| 24 | + |
| 25 | +from dataqs.helpers import style_exists |
| 26 | +from dataqs.processor_base import GeoDataProcessor |
| 27 | + |
| 28 | +script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 29 | + |
| 30 | + |
| 31 | +class LandscanProcessor(GeoDataProcessor): |
| 32 | + """ |
| 33 | + Class for processing sample Landscan data |
| 34 | + """ |
| 35 | + |
| 36 | + layer = "landscan" |
| 37 | + description = "LandScan 2011 Global Population Project" |
| 38 | + |
| 39 | + def get_landscan(self): |
| 40 | + """ |
| 41 | + Downloads the sample landscan image for Cyprus |
| 42 | + """ |
| 43 | + |
| 44 | + url = "http://web.ornl.gov/sci/landscan/" + \ |
| 45 | + "landscan2011/LS11sample_Cyprus.zip" |
| 46 | + |
| 47 | + zip_dir = os.path.join(self.tmp_dir, "landscan.zip") |
| 48 | + urlretrieve(url, zip_dir) |
| 49 | + |
| 50 | + # Open up the zip file |
| 51 | + zip_ref = zipfile.ZipFile(zip_dir, 'r') |
| 52 | + landscan_dir = os.path.join(self.tmp_dir, "landscan") |
| 53 | + zip_ref.extractall(landscan_dir) |
| 54 | + zip_ref.close() |
| 55 | + |
| 56 | + return landscan_dir |
| 57 | + |
| 58 | + def convert_landscan(self, landscan_dir): |
| 59 | + """ |
| 60 | + Converts arcgrid into a tiff file |
| 61 | + """ |
| 62 | + |
| 63 | + # Important |
| 64 | + # The directory which holds the .adf files |
| 65 | + # needs to be found here |
| 66 | + |
| 67 | + for subdir, dirs, files in os.walk(landscan_dir): |
| 68 | + for d in dirs: |
| 69 | + direc = os.listdir(os.path.join(subdir, d)) |
| 70 | + if set([f.endswith('.adf') for f in direc]) == {True}: |
| 71 | + grid_dir = os.path.join(subdir, d) |
| 72 | + |
| 73 | + src_ds = gdal.Open(grid_dir) |
| 74 | + proj = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs' |
| 75 | + src_ds.SetProjection(proj) |
| 76 | + driver = gdal.GetDriverByName("GTiff") |
| 77 | + |
| 78 | + # Output to new format |
| 79 | + tiff_output = os.path.join(self.tmp_dir, "landscan.tiff") |
| 80 | + driver.CreateCopy(tiff_output, src_ds, 0) |
| 81 | + |
| 82 | + return tiff_output |
| 83 | + |
| 84 | + def import_landscan(self, landscan_tiff): |
| 85 | + """ |
| 86 | + Imports landscan to geonode |
| 87 | + """ |
| 88 | + |
| 89 | + self.post_geoserver(landscan_tiff, self.layer) |
| 90 | + if not style_exists(self.layer): |
| 91 | + with open(os.path.join( |
| 92 | + script_dir, 'resources/landscan.sld')) as sld: |
| 93 | + self.set_default_style(self.layer, |
| 94 | + self.layer, sld.read()) |
| 95 | + self.update_geonode(self.layer, title=self.layer, |
| 96 | + store=self.layer, |
| 97 | + description=self.description) |
| 98 | + self.truncate_gs_cache(self.layer) |
| 99 | + |
| 100 | + def run(self): |
| 101 | + """ |
| 102 | + Retrieve and process Landscan sample data |
| 103 | + """ |
| 104 | + |
| 105 | + landscan_dir = self.get_landscan() |
| 106 | + landscan_tiff = self.convert_landscan(landscan_dir) |
| 107 | + self.import_landscan(landscan_tiff) |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + processor = LandscanProcessor() |
| 111 | + processor.run() |
0 commit comments