Wednesday, August 12, 2015

GPX to postgres bash script

To import a gpx file into postgres do this:

# ogr2ogr -append -f PostgreSQL "PG:dbname=ctbto user=postgres" 2016114.gpx

cool


And a bash script would look like this (compliments of Mapbox from http://www.mapbox.com/guides/postgis-manual/):

files=(
        2016114.gpx
        2016115.gpx
)
for file in ${files[@]}; do
        ogr2ogr -append -f PostgreSQL "PG:dbname=ctbto user=postgres" $file
done


or

for file in /dir/*.gpx
do
     ogr2ogr -append -f PostgreSQL "PG:dbname=ctbto user=postgres" $file
done


Adding a column while importing:

ogrinfo -f PostgreSQL "PG:dbname=your_db user=xxxx password=yyyy" -sql "UPDATE tracks SET track_id = 1234 WHERE ogc_fid = (SELECT max( ogc_fid ) FROM tracks)"

(from: http://gis.stackexchange.com/questions/101825/add-gpx-files-to-postgresql-database-and-add-one-column-with-a-given-value)

Friday, July 24, 2015

Coverting Radio Mobile coverage charts for use with Tilemill and QGIS


Radio mobile can produce radio transmission coverage charts based on a set of particulars related to a transmitter and a receiver. Such a thing is good to theoretically predict signal coverage in various terrain. Since the program can use SRTM data for terrain backgrounds it is reasonably useful when visualized on a GIS. The files produced when creating a coverage plot can be in various formats, .png, .tif, .kml etc but no geo-referencing data is embedded in the file so it cannot be dropped into QGIS or Tilemill for instance over a geo-referenced map background. External files containing the georeferencing data are produced but in my case I needed the data embedded in a single file.

I use the following GDAL commands to translate, warp and embed metadata in the image file so that it can be dropped into QGIS or Tilemill. I used a .png as an output from Radio Mobile which has a native alpha transparency channel so that the white parts of the picture become transparent. You can play with the various color schemes to get a good visual result. For Tilemill I used WGS84.

The radio Mobile setup was:

Picture Properties



Radio Plot Properties



Using the results of a plot named Picture16.png which is 1680 pixels by 1024 and Picture16.geo that was created while saving the picture file, use the the following command sets geographic positions for each corner pixel starting at top left (-gcp 0 0) moving clockwise, and writes to a file called temp.tif:


# gdal_translate -of GTiff -a_ullr 17.07981 49.19989 21.52019 47.40011 -a_srs EPSG:4326 -expand rgba Picture16.png output.tif



I hope someone finds this useful as I messed around for some time getting it to work, I'm sure there are other ways to get it done and don't hesitate to point out any better ways.

Monday, July 15, 2013

Problems with rendering shp files created by qgis

When importing shp files into tilemill I noticed some parts of a layer where not rendering. I went back to qgis and removed any null fields present in the original shp file within qgis.

Friday, October 26, 2012

Bash Script for Bulk Processing DEM files using GDAL and Mapbox

Just making some notes on how to work with some DEM files and create hillshade, slopeshade, color relief and clip via shape files. All is done using GDAL libraries and creates a set of files that can be overlayed in Mapbox's tilemill and then converted to mbtiles to be served by tileserver.

This is my bash script file to burn up a map of Cyprus. If anyone is interested, leave a comment and I'll throw up where to get and install the libraries, ASTERS and the mapbox tools. The basic sequence is as follows:


  1. merge all the ASTER DEM files to one big block covering all of Cyprus
  2. convert the projection to mercator 
  3. create hillshade from the DEM file, and clip
  4. create the color relief, and clip
  5. create slopeshade, and clip
  6. merge the hillshade and color relief, and clip
Note each type of file created is clipped with a shape file of Cyprus which allows me to use any file to overlay in tilemill and serve with tileserver on Ubuntu.



This was done on a Mac but has been used on a linux box (fedora and ubuntu).
source ~/.bash_profile
echo merging multiple aster files ASTGTM2_N34E032_dem.tif  into file [cyprus-aster.tif] ...
gdal_merge.py -o cyprus-aster.tif ASTGTM2_N34E032_dem.tif ASTGTM2_N34E033_dem.tif ASTGTM2_N34E034_dem.tif ASTGTM2_N34E035_dem.tif ASTGTM2_N35E032_dem.tif ASTGTM2_N35E033_dem.tif ASTGTM2_N35E034_dem.tif ASTGTM2_N35E035_dem.tif 
gdalinfo -mm cyprus-aster.tif
echo warping cyprus-aster.tif from 4326 to 3785 into file [cyprus-aster-3785.tif]....
gdalwarp -s_srs EPSG:4326 -t_srs EPSG:3785 -r lanczos cyprus-aster.tif cyprus-aster-3785.tif
gdalinfo -mm cyprus-aster-3785.tif
########### HILLSHADE ######
echo creating hillshade of cyprus-aster-3785.tif into file [hillshade.tif]....
gdaldem hillshade cyprus-aster-3785.tif hillshade.tif -compute_edges
gdalinfo -mm hillshade.tif
echo clipping hillshade inot file [hillshade-clipped.tif]...
rm -f hillshade-clipped.tif
gdalwarp -co compress=deflate -dstnodata 255 -cutline c.shp hillshade.tif hillshade-clipped.tif
############# COLOR RELIEF #############
echo creating color-relief of cyprus-aster.tif into file [relief.tif]
gdaldem color-relief cyprus-aster-3785.tif ramp-dark-white-red-green-khaki.txt relief.tif
echo clipping relief....
rm -f relief-clipped.tif
gdalwarp -co compress=lzw -dstalpha -cutline c.shp relief.tif relief-clipped.tif
################# SLOPE #############
echo making a slope relief
gdaldem slope cyprus-aster-3785.tif slope.tif 
#echo clipping slope....
rm -f slope-clipped.tif
gdalwarp -co compress=deflate -dstnodata 255 -cutline c.shp slope.tif slope-clipped.tif
echo adding slope from slop-ramp.txt into file slopeshade.tif
gdaldem color-relief slope.tif slope-ramp.txt slopeshade.tif

#echo clipping slopeshade....
rm -f slopeshade-clipped.tif
#gdalwarp -co compress=deflate -dstnodata 255 -cutline c.shp slopeshade.tif slopeshade-clipped.tif
gdalwarp -co compress=lzw -dstalpha -cutline c.shp slopeshade.tif slopeshade-clipped.tif

########## RELIEF AND HILLSHADE ##############

echo merging color-relief and hillshade into file [relief-hillshade.tif]
./hsv_merge.py relief.tif hillshade.tif relief-hillshade.tif

########## CLIPPING ################

echo removing any old files before creating the final version
rm -f cyprus-clipped.tif

echo cutting out cyprus and finishing... cutting out relief-hillshade.tif with cyp.shp into file [cyprus-clipped.tif]
gdalwarp -co compress=deflate -dstnodata 255 -cutline c.shp relief-hillshade.tif cyprus-clipped.tif