[Zoo-discuss] RE Default Processes of ZooProject

truongxuan quang truongxuanquang at yahoo.com
Fri Jul 25 02:53:04 PDT 2014


Hello

Yes!  I prefer to use GRASS, because  in near future if you need to extend you calculation in raster , vector and even for calculating complicated a various GIS model, I think GRASS can help you a lot in order to avoid, reduce and optimize your codes.

I am not so sure it is necessary to enclose my code here as example or not, but I hope it is useful for you. This example is NOT IN a standard way, My suggestion is using the WPS-GRASS-Bridge to solve your problem.

Bellow is the code to get Population from landscan image, this ZOO-Project WPS based on input data=[{single polygon or two polygons},  {landsacan Image}]
In case two polygons required, it means population calculation  over a area between two polygons 


#!/usr/bin/python python
import sys
import os 
import zoo
from shapely.wkt import loads
from geojson import dumps

def population(conf,inputs,outputs):
x= loads(inputs["a"]["value"])
Mpolygon=1
if inputs["b"]["value"]<>'NULL':
y=loads(inputs["b"]["value"])
geojson_string2=dumps(y)
Mpolygon=2
geojson_string=dumps(x)
fdx=open('/var/www/data/tempx.geojson', 'w+')
fdx.write(geojson_string)
fdx.close()
outputs["f1"]["value"]= geojson_string
if Mpolygon==2:
fdy=open('/var/www/data/tempy.geojson', 'w+')
        fdy.write(geojson_string2)
        fdy.close()
outputs["f2"]["value"]= geojson_string2
#outputs["f1"]["value"]= geojson_string
gisbase = os.environ['GISBASE'] ="/usr/local/src/grass6_devel/dist.x86_64-unknown-linux-gnu"
os.environ['HOME']='/home/quang'
gisdbase=os.path.join(os.environ['HOME'],"grass_data")
location="all"
mapset="PERMANENT"
sys.path.append(os.path.join(os.environ['GISBASE'], "etc", "python"))
import grass.script as grass
import grass.script.setup as gsetup
gsetup.init(gisbase, gisdbase, location, mapset)
vtest='test_vector0'
rtest='test_raster0'
vtest1='test_vector1'
vtest2='test_vector2'
from grass.script import core as comm

comm.run_command('g.region', rast='landscan at PERMANENT', quiet= True)
vec=''
if Mpolygon==2:
comm.run_command('v.in.ogr', dsn='/var/www/data/tempx.geojson', output='%s' %(vtest1), overwrite= True)#small rings
comm.run_command('v.in.ogr', dsn='/var/www/data/tempy.geojson', output='%s' %(vtest2), overwrite= True)# large rings
comm.run_command('v.overlay', ainput='%s' %(vtest2), atype='area', binput='%s' %(vtest1), output='%s' %(vtest), operator='not', overwrite= True)
comm.run_command('g.remove', vect='%s' %(vtest1))
comm.run_command('g.remove', vect='%s' %(vtest2))
else :
comm.run_command('v.in.ogr', dsn='/var/www/data/tempx.geojson', output='%s' %(vtest))
comm.run_command('g.region', vect='%s' %(vtest), quiet= True)
comm.run_command('v.to.rast', input= vtest, output= rtest, use ='val', value='1', overwrite= True)
comm.run_command('r.mask', flags='o', input=rtest)
population = comm.read_command('r.sum', rast='landscan at PERMANENT', quiet= True)
comm.run_command('r.mask', flags='r', quiet=True)
comm.run_command('g.remove', vect=vtest, rast=rtest)

outputs["Result"]["value"]="POP" + str(population)
return 3

If you have any doubt about GRASS, here is the link http://grass.osgeo.org/grass65/manuals/
 
 Best regards

Quang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/zoo-discuss/attachments/20140725/87cabd8c/attachment-0001.html>


More information about the Zoo-discuss mailing list