1
0
Fork 0

[grass scripts] minor clean up

This commit is contained in:
Scott Giese 2019-01-31 16:54:37 -06:00
parent f88863e4c2
commit 640c91b369
6 changed files with 46 additions and 69 deletions

View file

@ -33,6 +33,7 @@
#include "triangle.h"
void build_segments(struct triangulateio* tri, struct Map_info* map)
{
plus_t node_idx, line_idx;

View file

@ -24,7 +24,8 @@
#include <grass/dbmi.h>
#include <grass/vector.h>
void apply_setz_operator( struct Map_info* map, double z, int* cats, int ncats, int layer)
void apply_setz_operator(struct Map_info* map, double z, int* cats, int ncats, int layer)
{
const plus_t area_count = Vect_get_num_areas( map );
plus_t area_idx;
@ -42,7 +43,7 @@ void apply_setz_operator( struct Map_info* map, double z, int* cats, int ncats,
}
}
void apply_mean_operator( struct Map_info* map, int* cats, int ncats, int layer )
void apply_mean_operator(struct Map_info* map, int* cats, int ncats, int layer)
{
if (ncats > 0) {
/* We have to greedily collect adjacent selected areas */
@ -53,7 +54,7 @@ void apply_mean_operator( struct Map_info* map, int* cats, int ncats, int layer
}
}
void apply_slope_operator( struct Map_info* map, double slope, int* cats, int ncats, int layer)
void apply_slope_operator(struct Map_info* map, double slope, int* cats, int ncats, int layer)
{
if (ncats > 0) {
/* We apply slope to individual areas */
@ -73,9 +74,7 @@ int main(int argc, char *argv[])
struct Option *fieldopt;
int field;
struct Map_info oldmap, newmap;
struct field_info* fieldinfo;
dbDriver* driver;
int i, ncats, *cats;
int ncats, *cats;
G_gisinit(argv[0]);
@ -131,36 +130,36 @@ int main(int argc, char *argv[])
Vect_close(&oldmap);
if (whereopt->answer!=NULL) {
field = Vect_get_field_number( &newmap, fieldopt->answer );
fieldinfo = Vect_get_field( &newmap, field );
if (!fieldinfo) {
G_fatal_error(_("Database connection not defined for layer <%s>"),
fieldopt->answer);
}
G_debug(1, "Loading categories from table <%s>", fieldinfo->table);
driver = db_start_driver_open_database(fieldinfo->driver, fieldinfo->database);
if (driver == NULL) {
G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
fieldinfo->database, fieldinfo->driver);
}
ncats = db_select_int(driver, fieldinfo->table, fieldinfo->key, whereopt->answer,
&cats);
if (ncats == -1) {
G_fatal_error(_("Unable select records from table <%s>"), fieldinfo->table);
}
G_message(_("%d categories loaded from table <%s>"), ncats,
fieldinfo->table);
db_close_database(driver);
db_shutdown_driver(driver);
} else {
field = -1;
cats = NULL;
ncats = -1;
}
field = Vect_get_field_number( &newmap, fieldopt->answer );
struct field_info* fieldinfo = Vect_get_field( &newmap, field );
if (!fieldinfo) {
G_fatal_error(_("Database connection not defined for layer <%s>"),
fieldopt->answer);
}
G_debug(1, "Loading categories from table <%s>", fieldinfo->table);
dbDriver* driver = db_start_driver_open_database(fieldinfo->driver, fieldinfo->database);
if (driver == NULL) {
G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
fieldinfo->database, fieldinfo->driver);
}
ncats = db_select_int(driver, fieldinfo->table, fieldinfo->key, whereopt->answer,
&cats);
if (ncats == -1) {
G_fatal_error(_("Unable select records from table <%s>"), fieldinfo->table);
}
G_message(_("%d categories loaded from table <%s>"), ncats,
fieldinfo->table);
db_close_database(driver);
db_shutdown_driver(driver);
} else {
field = -1;
cats = NULL;
ncats = -1;
}
if (!strcmp(operator->answer, "setz")) {
apply_setz_operator( &newmap, atof(value->answer), cats, ncats, field );
@ -172,9 +171,10 @@ int main(int argc, char *argv[])
Vect_build(&newmap);
if (cats != NULL) {
G_free(cats);
}
if (cats != NULL) {
G_free(cats);
}
Vect_close(&newmap);
/* Don't forget to report to caller sucessful end of data processing :) */

View file

@ -21,10 +21,11 @@
#include <grass/glocale.h>
#include <grass/vector.h>
int main(int argc, char *argv[])
{
struct GModule *module; /* GRASS module for parsing arguments */
struct Option *old, *new;
struct Option *old;
struct Map_info oldmap;
G_gisinit(argv[0]);
@ -36,7 +37,7 @@ int main(int argc, char *argv[])
/* Define the different options as defined in gis.h */
old = G_define_standard_option(G_OPT_V_INPUT);
new = G_define_standard_option(G_OPT_F_OUTPUT);
G_define_standard_option(G_OPT_F_OUTPUT);
/* options and flags parser */
if (G_parser(argc, argv))

View file

@ -39,13 +39,6 @@ MODE="testing" # production, testing
InitCutoutTable () {
${PSQL} "DROP TABLE ${CUTLAYER}"
# ${PSQL} "CREATE TABLE ${CUTLAYER} (ogc_fid serial NOT NULL, \
# wkb_geometry geometry, \
# CONSTRAINT enforce_dims_wkb_geometry CHECK (ST_NDims(wkb_geometry) = 2), \
# CONSTRAINT enforce_geotype_wkb_geometry CHECK ((ST_GeometryType(wkb_geometry) = 'ST_Polygon'::text) \
# OR (ST_GeometryType(wkb_geometry) = 'ST_MultiPolygon'::text)), \
# CONSTRAINT enforce_srid_wkb_geometry CHECK (ST_SRID(wkb_geometry) = 4326) \
# )"
${PSQL} "CREATE TABLE ${CUTLAYER} (ogc_fid serial NOT NULL, \
wkb_geometry geometry, \
CONSTRAINT enforce_dims_wkb_geometry CHECK (ST_NDims(wkb_geometry) = 2), \

View file

@ -25,10 +25,11 @@ date
DUMPDIR=${HOME}/shp
mkdir -p ${DUMPDIR}
RUNDIR=`pwd`
cd `dirname ${0}` && export BASEDIR=`pwd`
cd ${RUNDIR}
CALLNAME=`basename ${0}`
#
MAPPINGFILE=${BASEDIR}/CORINEtoCS.txt
if [ ${CALLNAME} = "grassVMap.sh" ]; then
@ -50,33 +51,24 @@ elif [ ${CALLNAME} = "grassCLC06.sh" ]; then
else
SRCID=0
fi
PREFIX=${TYPE}${YEAR}
#PREFIX=nl
#PREFIX=clc${YEAR}_nl
CLEANMAP=cleanmap
if [ ${TYPE} = "v0" ]; then
SRCKBS=nad83
#SNAP=0.00001
SNAP=0.000001
#MIN_AREA1=1
MIN_AREA1=0.0000001
MIN_AREA=100
#MIN_AREA=0.000000001
elif [ ${TYPE} = "cs" ]; then
SRCKBS=wgs84
SNAP=0.000001
#MIN_AREA1=1
MIN_AREA1=0.0000001
MIN_AREA=1
elif [ ${TYPE} = "clc" ]; then
SRCKBS=laea
SNAP=0.01
#SNAP=0.001
#SNAP=0.0001
#SNAP=0.000001
MIN_AREA=1
#MIN_AREA=0.000000001
fi
########################################################################
@ -148,7 +140,6 @@ fn_importVMap() {
LAYER=`basename ${SHAPEFILE} | cut -f 1 -d \.`
MAP=`echo ${LAYER} | sed -e 's/-/_/g'`
g.remove vect=${MAP}
# v.in.ogr dsn="${LOADDIR}" layer=${LAYER} output=${MAP} snap=${SNAP} --verbose
v.in.ogr dsn="${LOADDIR}" layer=${LAYER} output=${MAP} --verbose
done
}
@ -238,11 +229,9 @@ fn_split() {
v.extract -t input=${LAYER} type=area output=${PREFIX}_mixedcrop_${ZONE} where="f_code NOT LIKE 'BH135' and veg = 0"
v.extract -t input=${LAYER} type=area output=${PREFIX}_drycrop_${ZONE} where="f_code NOT LIKE 'BH135' and veg = 1"
v.extract -t input=${LAYER} type=area output=${PREFIX}_irrcrop_${ZONE} where="f_code NOT LIKE 'BH135' and veg > 1"
# v.extract -t input=${LAYER} type=area output=${PREFIX}_rice_${ZONE} where="f_code LIKE 'BH135'"
;;
"grassa@veg(*)_area")
v.extract -t input=${LAYER} type=area output=${PREFIX}_grassland_${ZONE} where="f_code LIKE 'EB010'"
# v.extract -t input=${LAYER} type=area output=${PREFIX}_bamboo_${ZONE} where="f_code LIKE 'EC010'"
v.extract -t input=${LAYER} type=area output=${PREFIX}_scrub_${ZONE} where="f_code NOT LIKE 'EB010' AND f_code NOT LIKE 'EC010'"
;;
"oasisa@veg(*)_area")
@ -257,7 +246,6 @@ fn_split() {
v.extract -t input=${LAYER} type=area output=${PREFIX}_marsh_${ZONE} where="f_code NOT LIKE 'BH015'"
;;
"treesa@veg(*)_area")
# v.extract -t input=${LAYER} type=area output=${PREFIX}_mangrove_${ZONE} where="veg = 19"
v.extract -t input=${LAYER} type=area output=${PREFIX}_deciduousforest_${ZONE} where="veg = 24"
v.extract -t input=${LAYER} type=area output=${PREFIX}_evergreenforest_${ZONE} where="veg = 25"
v.extract -t input=${LAYER} type=area output=${PREFIX}_mixedforest_${ZONE} where="veg != 19 AND veg != 24 AND veg != 25"
@ -292,7 +280,6 @@ fn_reclass() {
done
g.remove vect=${OUTPUT}_patched
v.patch input=`g.mlist type=vect pattern="${OUTPUT}_[a-z][a-z][a-z]_lcclass" separator=,` output=${OUTPUT}_patched
# v.patch input=`g.mlist type=vect pattern="${OUTPUT}_[a-z][a-z][a-z]_lcclass" separator=,` output=${INTMAP}
g.remove vect=${OUTPUT}_bpol,${OUTPUT}_snap,${OUTPUT}_split,${OUTPUT}_rmsa,${OUTPUT}_rmdangle,${OUTPUT}_rmarea,${OUTPUT}_prune
v.clean input=${OUTPUT}_patched output=${OUTPUT}_bpol -c tool=bpol type=boundary --verbose
@ -451,7 +438,6 @@ fn_export() {
#
SELECTION=`v.category input=${CLEANMAP} type=centroid option=print | sort -n | uniq`
for CATEGORY in ${SELECTION}; do
# LAYER=${PREFIX}_${CATEGORY}
LAYER=`grep \^${CATEGORY} ${MAPPINGFILE} | awk '{print $2}' | sed -e "s/^cs_/${PREFIX}_/g"`
g.remove vect=${LAYER}
v.extract cats=${CATEGORY} input=${CLEANMAP} output=${LAYER} type=area
@ -461,7 +447,6 @@ fn_export() {
g.rename vect=${LAYER},${NEWLAYER}
LAYER=${NEWLAYER}
fi
# v.out.ogr input=${LAYER} type=area dsn=${DUMPDIR}/${PREFIX}_c${CATEGORY}.shp # For CLC
v.out.ogr input=${LAYER} type=area dsn=${DUMPDIR}/${LAYER}.shp # For VMap0/CS
fn_topostgis ${LAYER}
done

View file

@ -28,8 +28,6 @@ mkdir -p ${DUMPDIR}
export LD_LIBRARY_PATH=/opt/GRASS.32/lib
OGR2OGR=/opt/GRASS.32/bin/ogr2ogr
OGRINFO=/opt/GRASS.32/bin/ogrinfo
#OGR2OGR=/usr/local/bin/ogr2ogr
#OGRINFO=/usr/local/bin/ogrinfo
AREAS="v0eur_5/vmaplv0/eurnasia v0noa_5/vmaplv0/noamer v0sas_5/vmaplv0/sasaus v0soa_5/vmaplv0/soamafr"
@ -46,7 +44,6 @@ GetFeatureNames () {
ConvertToSHapefiles () {
for VMAP_URL in ${AREAS}; do
ZONE=`echo ${VMAP_URL} | cut -f 1 -d \/ | sed -e 's/^v0//g' -e 's/_5\$//g'`
# for FEATURE in `GetFeatureNames`; do
for FEATURE in `${OGRINFO} gltp:/vrf/home/martin/live/vmap0/${VMAP_URL} 2>&1 | egrep \^"[0-9].*@" | awk '{print $2}' | sort | uniq`; do
CATEGORY=`echo ${FEATURE} | cut -f 1 -d \@`
DIR=`echo ${FEATURE} | cut -f 1 -d \( | cut -f 2 -d \@`