[GRASS-SVN] r70395 - sandbox/martinl

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jan 18 08:49:52 PST 2017


Author: martinl
Date: 2017-01-18 08:49:52 -0800 (Wed, 18 Jan 2017)
New Revision: 70395

Added:
   sandbox/martinl/tiles-rast-stats-import.sh
Modified:
   sandbox/martinl/tiles-rast-stats.py
Log:
tiles-rast-stats (sandbox): add import sample bash script + fix script for FID columns (dirty way)

Added: sandbox/martinl/tiles-rast-stats-import.sh
===================================================================
--- sandbox/martinl/tiles-rast-stats-import.sh	                        (rev 0)
+++ sandbox/martinl/tiles-rast-stats-import.sh	2017-01-18 16:49:52 UTC (rev 70395)
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+# createdb tiles
+# psql tiles -c "create extension postgis"
+
+CONNSTRING="dbname=tiles"
+OUTPUT_LAYER="wrs2_descending"
+
+# import as multipolygons
+ ogr2ogr -f PostgreSQL -lco GEOMETRY_NAME=geom -nlt PROMOTE_TO_MULTI \
+     -nln ${OUTPUT_LAYER}_m "PG:${CONNSTRING}" -overwrite \
+     /vsizip/vsicurl/https://landsat.usgs.gov/sites/default/files/documents/wrs2_descending.zip
+
+# multipolygon -> polygon
+echo "\\\c ${CONNSTRING};
+
+drop table $OUTPUT_LAYER;
+create table $OUTPUT_LAYER as select *,(st_dump(geom)).geom as geom1 from ${OUTPUT_LAYER}_m;
+
+alter table $OUTPUT_LAYER drop column geom;
+alter table $OUTPUT_LAYER rename column geom1 to geom;
+create index on $OUTPUT_LAYER using gist (geom);
+
+alter table $OUTPUT_LAYER drop column ogc_fid;
+alter table $OUTPUT_LAYER add column ogc_fid serial;
+alter table $OUTPUT_LAYER add primary key(ogc_fid);
+
+alter table $OUTPUT_LAYER alter column geom type geometry(polygon, 4326);
+
+drop table ${OUTPUT_LAYER}_m" | psql
+
+# and now link
+v.external in="PG:${CONNSTRING}" layer=$OUTPUT_LAYER --o
+
+# test tiles-rast-stats.py
+
+echo "
+---------------------------------------------------------------------------
+Example:
+---------------------------------------------------------------------------
+
+wget http://training.gismentors.eu/geodata/eu-dem/dmt.zip
+unzip dmt.zip
+r.import in=dmt.tif output=dmt
+rm dmt.zip
+
+g.region rast=dmt
+./tiles-rast-stats.py -l dsn=\"PG:${CONNSTRING}\" layer=${OUTPUT_LAYER} rast=dmt keycolumn=ogc_fid np=6 method=average
+
+v.db.select $OUTPUT_LAYER where=\"dmt_average is not null\" columns=ogc_fid,dmt_average
+"
+
+exit 0
\ No newline at end of file


Property changes on: sandbox/martinl/tiles-rast-stats-import.sh
___________________________________________________________________
Added: svn:executable
   + *

Modified: sandbox/martinl/tiles-rast-stats.py
===================================================================
--- sandbox/martinl/tiles-rast-stats.py	2017-01-18 15:56:07 UTC (rev 70394)
+++ sandbox/martinl/tiles-rast-stats.py	2017-01-18 16:49:52 UTC (rev 70395)
@@ -136,9 +136,13 @@
             # improve filter
             geom = feature.GetGeometryRef()
             if geom.Intersects(filter_geom):
-                tiles.append(feature.GetField(keycolumn))
-    except ValueError:
-        grass.fatal("Column <{}> not found".format(keycolumn))
+                try:
+                    tiles.append(feature.GetField(keycolumn))
+                except:
+                    # TODO: test if keycolumn is really used as FID column by GDAL
+                    tiles.append(feature.GetFID())
+    except ValueError as e:
+        grass.fatal("{}".format(e))
     grass.message("{} tiles filtered".format(len(tiles)))
 
     return ds, layer, tiles



More information about the grass-commit mailing list