Skip to content

Commit

Permalink
support GRASS using existing location
Browse files Browse the repository at this point in the history
fix #408
  • Loading branch information
pesekon2 authored and jachym committed Feb 5, 2019
1 parent 904e2ad commit 4bc4850
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions pywps/app/Process.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ def _set_grass(self, wps_request):

if self.grass_location:

import random
import string
from grass.script import core as grass
from grass.script import setup as gsetup

Expand All @@ -329,6 +331,8 @@ def _set_grass(self, wps_request):
os.environ['GISRC'] = gisrc.name

new_loc_args = dict()
mapset_name = 'pywps_ms_{}'.format(
''.join(random.sample(string.ascii_letters, 5)))

if self.grass_location.startswith('complexinput:'):
# create new location from a georeferenced file
Expand All @@ -342,33 +346,47 @@ def _set_grass(self, wps_request):

if new_loc_args:
dbase = self.workdir
location = 'pywps_location'
location = str()
while os.path.isdir(os.path.join(dbase, location)):
location = 'pywps_loc_{}'.format(
''.join(random.sample(string.ascii_letters, 5)))

gsetup.init(self.workdir, dbase, location, 'PERMANENT')

grass.create_location(dbase=dbase,
location=location,
**new_loc_args)
LOGGER.debug('GRASS location based on {} created'.format(
list(new_loc_args.keys())[0]))
grass.run_command('g.mapset',
mapset=mapset_name,
flags='c',
dbase=dbase,
location=location)

# create temporary mapset within existing location
elif os.path.isdir(self.grass_location):
from grass.pygrass.gis import make_mapset

LOGGER.debug('Temporary mapset will be created')

dbase = os.path.dirname(self.grass_location)
location = os.path.basename(self.grass_location)

grass.run_command('g.gisenv', set="GISDBASE={}".format(dbase))
grass.run_command('g.gisenv', set="LOCATION_NAME=%s" % location)

while os.path.isdir(os.path.join(dbase, location, mapset_name)):
mapset_name = 'pywps_ms_{}'.format(
''.join(random.sample(string.ascii_letters, 5)))
make_mapset(mapset=mapset_name, location=location,
gisdbase=dbase)
grass.run_command('g.gisenv', set="MAPSET=%s" % mapset_name)

else:
raise NoApplicableCode('Location does exists or does not seem '
'to be in "EPSG:XXXX" form nor is it existing directory: {}'.format(location))

# copy projection files from PERMAMENT mapset to temporary mapset
mapset_name = 'pywps_mapset'
grass.run_command('g.mapset',
mapset=mapset_name,
flags='c',
dbase=dbase,
location=location)

# set _grass_mapset attribute - will be deleted once handler ends
self._grass_mapset = mapset_name

Expand Down

0 comments on commit 4bc4850

Please sign in to comment.