[GRASS-SVN] r40924 - in grass-addons/ossim_grass: . v.planet

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Feb 10 20:55:44 EST 2010


Author: epifanio
Date: 2010-02-10 20:55:44 -0500 (Wed, 10 Feb 2010)
New Revision: 40924

Added:
   grass-addons/ossim_grass/v.planet/
   grass-addons/ossim_grass/v.planet/Makefile
   grass-addons/ossim_grass/v.planet/description.html
   grass-addons/ossim_grass/v.planet/ogrTovrt.py
   grass-addons/ossim_grass/v.planet/v.planet.py
Log:
added v.planet.py module

Added: grass-addons/ossim_grass/v.planet/Makefile
===================================================================
--- grass-addons/ossim_grass/v.planet/Makefile	                        (rev 0)
+++ grass-addons/ossim_grass/v.planet/Makefile	2010-02-11 01:55:44 UTC (rev 40924)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.planet.py
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/ossim_grass/v.planet/description.html
===================================================================
--- grass-addons/ossim_grass/v.planet/description.html	                        (rev 0)
+++ grass-addons/ossim_grass/v.planet/description.html	2010-02-11 01:55:44 UTC (rev 40924)
@@ -0,0 +1,67 @@
+<H2>DESCRIPTION</H2>
+
+
+<EM><B>v.planet.py : </B></EM> Scripts to generate vrt layer from a selected grass vector layer and add/zoomTo/remove the data to ossimplanet using the ossimplanet tcp listner.
+
+<H2>OPTIONS</H2>
+
+<p>
+<B>[-a] :</B> add map and zoom to it
+<p>
+<B>[-r] :</B> remove map
+<p>
+<B>[map] :</B> input layer
+<p>
+<p>
+<B>[host] :</B> ossimplanet listner settings "host" (default to "loacalhost")
+<p>
+<p>
+<B>[pport] :</B> ossimplanet listner settings "position message port number" (default to "7000")
+<p>
+<p>
+<B>[dport] :</B> ossimplanet listner settings "data port number" (default to "8000")
+<p>
+<p>
+<B>[brush] :</B> brush color -  syntax : 'r,g,b'
+<p>
+<p>
+<B>[pen] :</B> pen color - syntax : 'r,g,b'
+<p>
+<p>
+<B>[size] :</B> point size -  'x,y'
+<p>
+<p>
+<B>[fill] :</B> fill polygons - [0 / 1]
+<p>
+<p>
+<B>[thickness] :</B> thickness - [0 / 1]
+<p>
+
+<H2>EXAMPLE</H2>
+
+Add selected map to ossimplanet screen and zoom to it :
+<div class="code"><pre>
+v.planet.py map=elevation.10m -a
+</pre></div>
+<p>
+Remove selected map from ossimplanet screen :
+<div class="code"><pre>
+v.planet.py map=elevation.10m -r
+</pre></div>
+<p>
+
+
+<H2>REFERENCES</H2>
+ 
+<EM><A HREF="http://web.me.com/epiesasha/PlanetSasha/">PlanetSasha</A></EM> Google Summer of Code - OSGEO - Massimo Di Stefano.
+
+<H2>SEE ALSO</H2>
+
+<EM><A HREF="r.planet.html">r.planet.py</A></EM>
+
+<H2>AUTHOR</H2>
+
+Massimo Di Stefano - <EM><A HREF="mailto:massimodisasha at gmail.com">@-mail</A></EM> <br>
+
+
+<p><i>Last changed: $Date: 2010-02-07 00:45:10 +0200 (Mon, 9 Feb 2010) $</i>

Added: grass-addons/ossim_grass/v.planet/ogrTovrt.py
===================================================================
--- grass-addons/ossim_grass/v.planet/ogrTovrt.py	                        (rev 0)
+++ grass-addons/ossim_grass/v.planet/ogrTovrt.py	2010-02-11 01:55:44 UTC (rev 40924)
@@ -0,0 +1,137 @@
+#!/usr/bin/env python
+################################################################################
+#
+# MODULE:       r.planet.py
+#
+# AUTHOR(S):    Massimo Di Stefano 2010-02-7 - massimodisasha at yahoo.it
+#               (original code : ogr2vrt.py - Frank Warmerdam, warmerdam at pobox.com)
+#               
+#
+# PURPOSE:      Create OGR VRT from source datasource and 
+#               OMD associated style-file
+#
+# COPYRIGHT:    (c) 2010 by Massimo Di Stefano 
+#
+#               This program is free software under the GNU General Public
+#               License (>=v2). Read the file COPYING that comes with GRASS
+#               for details.
+#
+# REQUIRES:     Gdal - python
+#                 
+#               
+#
+################################################################################
+
+
+try:
+	from osgeo import osr, ogr, gdal
+except ImportError:
+	import osr, ogr, gdal
+
+import string
+import sys
+
+def GeomType2Name( type ):
+	if type == ogr.wkbUnknown:
+		return 'wkbUnknown'
+	elif type == ogr.wkbPoint:
+		return 'wkbPoint'
+	elif type == ogr.wkbLineString:
+		return 'wkbLineString'
+	elif type == ogr.wkbPolygon:
+		return 'wkbPolygon'
+	elif type == ogr.wkbMultiPoint:
+		return 'wkbMultiPoint'
+	elif type == ogr.wkbMultiLineString:
+		return 'wkbMultiLineString'
+	elif type == ogr.wkbMultiPolygon:
+		return 'wkbMultiPolygon'
+	elif type == ogr.wkbGeometryCollection:
+		return 'wkbGeometryCollection'
+	elif type == ogr.wkbNone:
+		return 'wkbNone'
+	elif type == ogr.wkbLinearRing:
+		return 'wkbLinearRing'
+	else:
+		return 'wkbUnknown'
+		
+def Esc(x):
+	return gdal.EscapeString( x, gdal.CPLES_XML )
+
+def makestile(outfile, brush, pen, size, fill, thickness):
+	brush = brush.split(',')
+	pen = pen.split(',')
+	size = size.split(',')
+	outfile = outfile.replace('.vrt','')
+	outfile = outfile+'.omd'
+	omd = '// vector file rendering options\n'
+	omd += 'brush_color: %s %s %s \n' % (brush[0], brush[1], brush[2])
+	omd += 'pen_color: %s %s %s \n' % (pen[0], pen[1], pen[2])
+	omd += 'point_width_height: %s %s \n' % (size[0], size[1])
+	omd += 'fill_flag: %s \n' % (fill)
+	omd += 'thickness: %s \n' % (thickness)
+	open(outfile,'w').write(omd)
+
+def ogrvrt(infile,outfile):
+	layer_list = []
+	relative = "0"
+	schema=0
+	src_ds = ogr.Open( infile, update = 0 )
+	if len(layer_list) == 0:
+		for layer in src_ds:
+			layer_list.append( layer.GetLayerDefn().GetName() )
+	vrt = '<OGRVRTDataSource>\n'
+	for name in layer_list:
+		layer = src_ds.GetLayerByName(name)
+		layerdef = layer.GetLayerDefn()
+		vrt += '  <OGRVRTLayer name="%s">\n' % Esc(name)
+		vrt += '    <SrcDataSource relativeToVRT="%s" shared="%d">%s</SrcDataSource>\n' \
+		% (relative,not schema,Esc(infile))
+		if schema:
+			vrt += '    <SrcLayer>@dummy@</SrcLayer>\n' 
+		else:
+			vrt += '    <SrcLayer>%s</SrcLayer>\n' % Esc(name)
+		vrt += '    <GeometryType>%s</GeometryType>\n' \
+		% GeomType2Name(layerdef.GetGeomType())
+		srs = layer.GetSpatialRef()
+		if srs is not None:
+			vrt += '    <LayerSRS>%s</LayerSRS>\n' \
+			% (Esc(srs.ExportToWkt()))
+		# Process all the fields.
+		for fld_index in range(layerdef.GetFieldCount()):
+			src_fd = layerdef.GetFieldDefn( fld_index )
+			if src_fd.GetType() == ogr.OFTInteger:
+				type = 'Integer'
+			elif src_fd.GetType() == ogr.OFTString:
+				type = 'String'
+			elif src_fd.GetType() == ogr.OFTReal:
+				type = 'Real'
+			elif src_fd.GetType() == ogr.OFTStringList:
+				type = 'StringList'
+			elif src_fd.GetType() == ogr.OFTIntegerList:
+				type = 'IntegerList'
+			elif src_fd.GetType() == ogr.OFTRealList:
+				type = 'RealList'
+			elif src_fd.GetType() == ogr.OFTBinary:
+				type = 'Binary'
+			elif src_fd.GetType() == ogr.OFTDate:
+				type = 'Date'
+			elif src_fd.GetType() == ogr.OFTTime:
+				type = 'Time'
+			elif src_fd.GetType() == ogr.OFTDateTime:
+				type = 'DateTime'
+			else:
+				type = 'String'
+				
+			vrt += '    <Field name="%s" type="%s"' \
+			% (Esc(src_fd.GetName()), type)
+			if not schema:
+				vrt += ' src="%s"' % Esc(src_fd.GetName())
+			if src_fd.GetWidth() > 0:
+				vrt += ' width="%d"' % src_fd.GetWidth()
+			if src_fd.GetPrecision() > 0:
+				vrt += ' precision="%d"' % src_fd.GetPrecision()
+			vrt += '/>\n'
+		vrt += '  </OGRVRTLayer>\n'
+	vrt += '</OGRVRTDataSource>\n' 
+	open(outfile,'w').write(vrt)

Added: grass-addons/ossim_grass/v.planet/v.planet.py
===================================================================
--- grass-addons/ossim_grass/v.planet/v.planet.py	                        (rev 0)
+++ grass-addons/ossim_grass/v.planet/v.planet.py	2010-02-11 01:55:44 UTC (rev 40924)
@@ -0,0 +1,279 @@
+#!/usr/bin/env python
+################################################################################
+#
+# MODULE:       r.planet.py
+#
+# AUTHOR(S):    Massimo Di Stefano 2010-02-7
+#               
+#
+# PURPOSE:      
+#
+# COPYRIGHT:    (c) 2010 by the GRASS Development Team
+#
+#               This program is free software under the GNU General Public
+#               License (>=v2). Read the file COPYING that comes with GRASS
+#               for details.
+#
+# REQUIRES:     Ossimplanet
+#                 
+#               
+#
+################################################################################
+
+#%module
+#% description: 
+#% keywords: vector
+#%end
+#%option
+#% key: map
+#% type: string
+#% gisprompt: old,vector,vector
+#% key_desc: name
+#% description: Name of vector map 
+#% required: yes
+#%end
+#%option
+#% key: host
+#% type: string
+#% key_desc: host
+#% answer: 127.0.0.1
+#% description: Host
+#% required : no
+#%end
+#%option
+#% key: dport
+#% type: double
+#% key_desc: dport
+#% answer: 8000
+#% description: Data Port 
+#% required : no
+#%end
+#%option
+#% key: pport
+#% type: double
+#% key_desc: port
+#% answer: 7000
+#% description: Position Port 
+#% required : no
+#%end
+#%flag
+#% key: a
+#% description: Add vector
+#%END
+#%flag
+#% key: r
+#% description: Remove vector
+#%END
+#%option
+#% key: brush
+#% type: string
+#% key_desc: brush
+#% description: brush color  
+#% required : no
+#%end
+#%option
+#% key: pen
+#% type: string
+#% key_desc: pen
+#% description: brush color  
+#% required : no
+#%end
+#%option
+#% key: size
+#% type: string
+#% key_desc: size
+#% description: size 
+#% required : no
+#%end
+#%option
+#% key: fill
+#% type: string
+#% key_desc: fill
+#% description: fill 
+#% required : no
+#%end
+#%option
+#% key: thickness
+#% type: string
+#% key_desc: thickness
+#% description: thickness 
+#% required : no
+#%end
+
+import sys
+import os
+import socket
+import grass.script as grass
+import string
+
+try:
+	from osgeo import osr, ogr, gdal
+except ImportError:
+	import osr, ogr, gdal
+
+from ogrTovrt import ogrvrt, makestile
+
+
+def main():
+	add = flags['a']
+	remove = flags['r']
+	host = options['host']
+	dport = options['dport']
+	pport = options['pport']
+	grassenv = grass.gisenv()
+	mappa = options['map'].replace("@"," ")
+	mappa = mappa.split()
+	nflags = len(filter(None, [add, remove]))
+	if nflags > 1:
+		grass.run_command('g.message' , message = 'Cannot add & remove a map at same time.')
+	if nflags < 1:
+		grass.run_command('g.message' , message = 'No action requested , please choose one from "-a : add" or "-r : remove" flags.')
+	try :
+		vectorpath = os.path.join(grassenv['GISDBASE'], grassenv['LOCATION_NAME'], mappa[1], 'vector' , mappa[0] ) 
+		mapfile = os.path.join(vectorpath, 'head')
+		vrtdir = os.path.join(grassenv['GISDBASE'], grassenv['LOCATION_NAME'], mappa[1], 'vrt', 'vector')
+	except :
+		vectorpath = os.path.join(grassenv['GISDBASE'], grassenv['LOCATION_NAME'], grassenv['MAPSET'], mappa[0] )
+		mapfile = os.path.join(vectorpath, 'head' )   
+		vrtdir = os.path.join(grassenv['GISDBASE'], grassenv['LOCATION_NAME'], grassenv['MAPSET'], 'vrt', 'vector')
+	d = os.path.dirname(vrtdir)
+	if not os.path.exists(d):
+		os.makedirs(d)
+	vrtfilename =  mappa[0] + '.vrt'
+	output = os.path.join(vrtdir, vrtfilename)
+	if not os.path.exists(output):
+		ogrvrt(mapfile,output)
+		makestile(output, options['brush'], options['pen'], options['size'], options['fill'], options['thickness'])
+	proj_info = projinfo()
+	unit = proj_info['units']
+	if unit.lower() == 'meters':
+		zoom_position = setCPRJ(options['map'])
+	if unit == 'Degrees':
+		zoom_position = setCLL(options['map'])
+	lat = zoom_position[0]
+	lon = zoom_position[1]
+	distance = zoom_position[2]
+	if add :
+		addzoom(output,lon,lat,distance,host,dport,pport)
+		print 'Added vector file :', mappa[0]
+		print 'Camera positioned to : '
+		print 'Longitude = ',lon
+		print 'Latitude = ', lat
+		print 'Altitude = ' , distance
+	if remove :
+		removefile(output,host,dport)
+		print 'Removed vector file :', mappa[0]
+
+
+def addfile(output,host,dport):
+	ossim_data_xml = "<Add target=':idolbridge'><Image groupType='groundTexture'><filename>%s</filename> <id>%s</id><name>%s</name></Image></Add>" % (output,output,output)
+	ossimdata = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+	ossimdata.connect((host, int(dport)))
+	ossimdata.send(ossim_data_xml)
+	ossimdata.close()
+
+
+def zoomto(lon,lat,distance,host,pport):
+	ossim_zoom_xml = '<Set target=":navigator" vref="wgs84"><Camera><longitude>%s</longitude><latitude>%s</latitude><altitude>%s</altitude><heading>0</heading><pitch>0</pitch><roll>0</roll><altitudeMode>absolute</altitudeMode><range>%s</range></Camera></Set>' % (lon, lat, distance, distance)
+	ossimposition = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+	ossimposition.connect((host, int(pport)))  
+	ossimposition.send(ossim_zoom_xml)
+	ossimposition.close()
+
+
+def removefile(output,host,dport):
+	ossimdata = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+	ossimdata.connect((host, int(dport)))
+	ossim_data_xml = "<Remove target=':idolbridge' id='%s' />" % (output)
+	ossimdata.send(ossim_data_xml)
+	ossimdata.close()
+
+
+def addzoom(output,lon,lat,distance,host,dport,pport):
+	addfile(output,host,dport)
+	zoomto(lon,lat,distance,host,pport)
+
+
+def projinfo():
+	units = grass.read_command("g.proj", flags='p')
+	units = units.replace('-','')
+	units = grass.parse_key_val(units, ':')
+	units_key = units.keys()
+	for i in units_key :
+		key_value = str(units[i]).strip()
+		units[i] = key_value
+	return units
+
+
+
+def setCLL(map):
+	center = []
+	info_region = grass.read_command('g.region', flags = 'cael', vect = '%s' % (map)) 
+	dict_region = grass.parse_key_val(info_region, ':')
+	lon = dict_region['east-west center']	
+	lat = dict_region['north-south center']
+	lon = str(lon)
+	lat = str(lat)
+	lon = lon.replace(':', " ")
+	lat = lat.replace(':', " ")
+	if lat[-1] == 'N':
+		signlat = 1
+	if lat[-1] == 'S':
+		signlat = -1
+	if lon[-1] == 'E':
+		signlon = 1
+	if lon[-1] == 'W':
+		signlon = -1
+	lat = lat[:-1] 
+	lon = lon[:-1]
+	lat = [float(i) for i in lat.split()]
+	lon = [float(i) for i in lon.split()]
+	lat = (lat[0] + (lat[1] / 60) + lat[2] / 3600) * float(signlat)
+	lon = (lon[0] + (lon[1] / 60) + lon[2] / 3600) * float(signlon)
+	ns = float(dict_region['north-south extent'])
+	we = float(dict_region['east-west extent'])
+	distance = (ns + we) / 2
+	center.append(lat)
+	center.append(lon)
+	center.append(distance)
+	return center
+
+
+def setCPRJ(map):
+	center = []
+	info_region = grass.read_command('g.region', flags = 'ael', vect = '%s' % (map)) 
+	dict_region = grass.parse_key_val(info_region, ':')
+	lon = dict_region['center longitude']	
+	lat = dict_region['center latitude']
+	lon = str(lon)
+	lat = str(lat)
+	lon = lon.replace(':', " ")
+	lat = lat.replace(':', " ")
+	if lat[-1] == 'N':
+		signlat = 1
+	if lat[-1] == 'S':
+		signlat = -1
+	if lon[-1] == 'E':
+		signlon = 1
+	if lon[-1] == 'W':
+		signlon = -1
+	lat = lat[:-1] 
+	lon = lon[:-1]
+	lat = [float(i) for i in lat.split()]
+	lon = [float(i) for i in lon.split()]
+	lat = (lat[0] + (lat[1] / 60) + lat[2] / 3600) * float(signlat)
+	lon = (lon[0] + (lon[1] / 60) + lon[2] / 3600) * float(signlon)
+	ns = float(dict_region['north-south extent'])
+	we = float(dict_region['east-west extent'])
+	distance = (ns + we) / 2
+	center.append(lat)
+	center.append(lon)
+	center.append(distance)
+	return center
+
+
+if __name__ == "__main__":
+	options, flags = grass.parser()
+	sys.exit(main())
+
+


Property changes on: grass-addons/ossim_grass/v.planet/v.planet.py
___________________________________________________________________
Added: svn:executable
   + *



More information about the grass-commit mailing list