improve override, open file in PIL for processing
This commit is contained in:
parent
aceae53030
commit
28c00e18a5
@ -5,6 +5,10 @@ import logging
|
||||
import stat
|
||||
import os
|
||||
from os.path import exists
|
||||
import requests
|
||||
import shutil
|
||||
from PIL import Image
|
||||
from io import BytesIO
|
||||
|
||||
import ee
|
||||
import sys
|
||||
@ -16,6 +20,35 @@ from country_iso2ify import get_resolver
|
||||
|
||||
resolver = get_resolver()
|
||||
|
||||
def create_elevation_file_from_region_name(target_region_name, target_filename):
|
||||
ee.Initialize(project="arizona-topo")
|
||||
elv = ee.Image('USGS/SRTMGL1_003')
|
||||
region_of_interest = ee.Geometry.BBox(b_list[0],
|
||||
b_list[1],
|
||||
b_list[2],
|
||||
b_list[3])
|
||||
countries = ee.FeatureCollection('FAO/GAUL/2015/level1').select('ADM0_NAME')
|
||||
# logging.debug("Countries are [{}]".format(countries))
|
||||
states = ee.FeatureCollection('FAO/GAUL/2015/level1').select('ADM1_NAME')
|
||||
|
||||
target_boundary = countries.filter(ee.Filter.eq('ADM0_NAME', target_region_name))
|
||||
# logging.debug("target_boundary is [{}]".format(target_boundary))
|
||||
|
||||
elevation_image = elv.updateMask(elv.gt(0))
|
||||
elevation_clip = elevation_image.clip(target_boundary)
|
||||
url = elevation_clip.getThumbUrl({
|
||||
'min': -300, 'max': 8500, 'region': region_of_interest, 'dimensions': 1024,
|
||||
'crs': 'EPSG:4326',
|
||||
'fileFormat': 'GeoTIFF',
|
||||
})
|
||||
logging.debug("url is [{}]".format(url))
|
||||
print("Downloading image from [{}]".format(url))
|
||||
response = requests.get(url, stream=True)
|
||||
with open(target_filename, 'wb') as out_file:
|
||||
shutil.copyfileobj(response.raw, out_file)
|
||||
del response
|
||||
|
||||
|
||||
def list_subunits_and_exit(country_name):
|
||||
iso_code = resolver.resolve(country_name)
|
||||
countries = country_subunits_by_iso_code(iso_code)
|
||||
@ -114,7 +147,7 @@ parser.add_argument(
|
||||
action='count',
|
||||
default=0,
|
||||
required=False,
|
||||
help="will not refetch an existing file unless this option is specified")
|
||||
help="will not refetch an existing elevation image file unless this option is specified")
|
||||
|
||||
parser.add_argument(
|
||||
"-c",
|
||||
@ -185,33 +218,11 @@ target_filename = target_region_name + ".png"
|
||||
if os.path.exists(target_filename):
|
||||
logging.debug("The file [{}] already exists.".format(target_filename))
|
||||
if args.override_existing == 0:
|
||||
print("The file [{}] already exists. -O flag not specified. Exiting.".format(target_filename))
|
||||
sys.exit(5)
|
||||
print("The file [{}] already exists. -O flag not specified. Using existing file.".format(target_filename))
|
||||
else:
|
||||
logging.debug("The file [{}] does not exist.".format(target_filename))
|
||||
logging.debug("Creating file [{}]".format(target_filename))
|
||||
with open(target_filename, 'w') as f:
|
||||
pass
|
||||
create_elevation_file_from_region_name(target_region_name, target_filename)
|
||||
|
||||
ee.Initialize(project="arizona-topo")
|
||||
elv = ee.Image('USGS/SRTMGL1_003')
|
||||
region_of_interest = ee.Geometry.BBox(b_list[0],
|
||||
b_list[1],
|
||||
b_list[2],
|
||||
b_list[3])
|
||||
countries = ee.FeatureCollection('FAO/GAUL/2015/level1').select('ADM0_NAME')
|
||||
#logging.debug("Countries are [{}]".format(countries))
|
||||
states = ee.FeatureCollection('FAO/GAUL/2015/level1').select('ADM1_NAME')
|
||||
|
||||
target_boundary = countries.filter(ee.Filter.eq('ADM0_NAME', args.country))
|
||||
#logging.debug("target_boundary is [{}]".format(target_boundary))
|
||||
pil_image = Image.open(target_filename)
|
||||
|
||||
elevation_image = elv.updateMask(elv.gt(0))
|
||||
elevation_clip = elevation_image.clip(target_boundary)
|
||||
url = elevation_clip.getThumbUrl({
|
||||
'min': -300, 'max': 8500, 'region': region_of_interest, 'dimensions': 1024,
|
||||
'crs': 'EPSG:4326',
|
||||
'fileFormat': 'GeoTIFF',
|
||||
})
|
||||
logging.debug("url is [{}]".format(url))
|
||||
print("url is [{}]".format(url))
|
||||
Loading…
x
Reference in New Issue
Block a user