Skip to content

Instantly share code, notes, and snippets.

@petrasovaa
Created April 13, 2017 19:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save petrasovaa/d4c2bdc680c94e4a9839a8c70b0d2d50 to your computer and use it in GitHub Desktop.
Save petrasovaa/d4c2bdc680c94e4a9839a8c70b0d2d50 to your computer and use it in GitHub Desktop.
Analyses files for US-IALE demo planting application
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 28 17:32:38 2017
@author: tangible
"""
import os
import shutil
import glob
from os.path import expanduser
from datetime import datetime
import analyses
from tangible_utils import get_environment
from experiment import updateDisplay
import grass.script as gscript
from grass.exceptions import CalledModuleError
trees = {1: "class1",
2: "class2",
3: "mixed",
4: "class3",
5: "class4"}
def run_patches(real_elev, scanned_elev, eventHandler, env, **kwargs):
group = 'color'
segment = 'segment'
signature = 'signature'
classification = 'classification'
filtered_classification = 'fclassification'
reject = 'reject'
patches = 'patches'
# detect patches
gscript.run_command('i.superpixels.slic', group=group, output=segment, compactness=2,
minsize=50, env=env)
gscript.run_command('i.smap', group=group, subgroup=group, signaturefile=signature,
output=classification, goodness=reject, env=env)
percentile = float(gscript.parse_command('r.univar', flags='ge', map=reject, env=env)['percentile_90'])
gscript.mapcalc('{new} = if({classif} < {thres}, {classif}, null())'.format(new=filtered_classification,
classif=classification, thres=percentile), env=env)
gscript.run_command('r.stats.quantile', base=segment, cover=filtered_classification, output=patches, env=env)
gscript.mapcalc('{} = int({})'.format(patches + '_cell', patches), env=env)
gscript.run_command('r.to.vect', flags='svt', input=patches + '_cell', output=patches, type='area', env=env)
base_cat = 6
#
# r.li
#
indices = 'index_'
# write config file if it doesn't exist
rlipath = os.path.join(expanduser("~"), ".grass7", "r.li")
configpath = os.path.join(rlipath, "patches")
outputpath = os.path.join(rlipath, "output")
if not os.path.exists(configpath):
with open(configpath, 'w') as f:
f.write('SAMPLINGFRAME 0|0|1|1\n')
f.write('SAMPLEAREA 0.0|0.0|1|1')
results = {}
results_list = []
# TODO: scaling values
gscript.mapcalc('{p2} = if({p} != {cl}, {p}, null())'.format(p2=patches + '2', p=patches, cl=base_cat), env=env)
for index in ['patchnum', 'richness', 'mps', 'shannon', 'shape']:
gscript.run_command('r.li.' + index, input=patches + '2', output=indices + index, config=configpath, env=env)
with open(os.path.join(outputpath, indices + index), 'r') as f:
results[index] = float(f.readlines()[0].strip().split('|')[-1])
if index == 'mps':
results[index] *= 10
results_list.append(results[index])
# update dashboard
event = updateDisplay(value=results_list)
eventHandler.postEvent(receiver=eventHandler.experiment_panel, event=event)
print results
# export patches
gscript.mapcalc('scanned_scan_int = int(transfer_dem)', env=env)
env2 = get_environment(raster=patches, res=10)
try:
gscript.run_command('r.mask', flags='r', env=env)
except:
pass
cats = gscript.read_command('r.describe', flags='1ni', map=patches + '_cell', env=env).strip()
cats = [int(cat) for cat in cats.splitlines()]
toexport = []
for cat in cats:
if cat == base_cat:
continue
gscript.run_command('r.mask', raster=patches + '_cell', maskcats=cat, env=env)
gscript.run_command('r.to.vect', flags='svt', input='scanned_scan_int', output=patches + '_2d', type='area', env=env2)
gscript.run_command('r.mask', flags='r', env=env)
gscript.run_command('v.drape', input=patches + '_2d', output='patch_' + trees[cat], elevation='scanned_scan_int', env=env2)
toexport.append('patch_' + trees[cat])
send_file('empty.txt')
export_polygons(vectors=toexport, env=env)
def export_polygons(vectors, env):
time = datetime.now()
suffix = '_{}_{}_{}'.format(time.hour, time.minute, time.second)
outdir = '/run/user/1000/gvfs/smb-share:server=192.168.0.2,share=usiale/Watch'
tmp_outdir = '/tmp'
for vector in vectors:
try:
gscript.run_command('v.out.ogr', input=vector, lco="SHPT=POLYGONZ",
output=tmp_outdir + '/' + vector + suffix + '.shp', env=env)
except CalledModuleError, e:
print e
for vector in vectors:
print 'attempt to copy to Payams'
for each in glob.glob(tmp_outdir + '/' + vector + suffix + '.*'):
print each
ext = each.split('.')[-1]
try:
shutil.copyfile(each, outdir + '/' + vector + suffix + '.' + ext)
except OSError as e:
if e.errno == 95:
pass
def send_file(name):
outdir = '/run/user/1000/gvfs/smb-share:server=192.168.0.2,share=usiale/Watch'
with open(os.path.join(outdir, name), 'w') as f:
f.close()
{
"taskDir": "/home/tangible/analyses/USIALE",
"logDir": "/tmp/",
"slides": {},
"tasks": [
{
"title": "Blender patches",
"layers": [
],
"base": "transfer_dem",
"calibrate": false,
"time_limit": 100000,
"scanning_params": {"smooth": 10, "numscans": 2, "zexag": 1},
"analyses": "blender_patches.py",
"filter" : {"threshold": 2000, "debug": true},
"single_scan": true,
"display": {"multiple": true, "average": 2, "size": [600, 300], "position": [3150, 710],
"fontsize": 14, "maximum": [20, 6, 60, 2.5, 8],
"formatting_string": ["{:g}", "{:g}","{:.1f}","{:.2f}","{:.1f}"],
"title": ["Patch #", "Richness", "Mean size", "Shannon", "Shape ind." ]}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment