#!/usr/bin/env python """ v.to.site.area -- export area label points and category labels into site. Copyright (c) 2003,2004 Matej Cepl Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. $Id: v.to.sites.area,v 1.6 2004/10/30 22:12:33 matej Exp $ """ from sys import exit,argv,stderr from os import environ,popen,sep from os.path import abspath,exists def errorexit(msg,exitflag): stderr.write(msg) stderr.write("\nUse --help to see valid options and arguments\n") exit(exitflag) if len(environ["GISBASE"])==0: print "You must be in GRASS GIS to run this program." exit(1) inmap=argv[1] #lazy to do proper parsin :-) try: p=popen('g.gisenv get=GISDBASE','r') location=p.read()[0:-1] p=popen('g.gisenv get=LOCATION_NAME','r') location=location+sep+p.read()[0:-1] p=popen('g.gisenv get=MAPSET','r') location=location+sep+p.read()[0:-1] p.close() except: errorexit("Cannot get value of the current LOCATION.",10) inlabfilename=abspath(location+'/dig_cats/'+inmap) incatfilename=abspath(location+'/dig_att/'+inmap) # Read category label file try: inlabfile=open(inlabfilename,'r') inlab=inlabfile.readlines() inlabfile.close() except: errorexit("Cannot read category label file.",5) # Read category attribute file try: incatfile=open(incatfilename,'r') incat=incatfile.readlines() incatfile.close() except: errorexit("Cannot read category attribute file.",5) # Open site file if not(exists(location+'/site_lists/')): mkdir(location+'/site_lists/') outfilename=abspath(location+'/site_lists/'+inmap) try: outfile=open(outfilename,'w') except: errorexit("Cannot open output site file.",5) cats={} # east,north for catno in range(len(incat)): line=incat[catno].split() if line[0]=="A": cats[str(line[3])]=line[1],line[2] # Prepare reading category label file line=inlab[0].split() labnomax=line[1] if labnomax==1: raise EOFError, "No labels available." title=inlab[1][0:-1] if len(title)!=0: title="-"+title #Print the header of file outfile.write("#generated by "+argv[0]+"\n") outfile.write("name|"+inmap+title+"\n") outfile.write("desc|sites of "+inmap+"\n") outfile.write("labels|Easting|Northing|%No Label\n") for labno in range(4,len(inlab)): line=inlab[labno].split(':') id=str(line[0]) meow=cats[id] label=str(line[1]) outfile.write(meow[0]+"|"+meow[1]+"|#"+id+" @"+label) try: outfile.close() except: errorexit("Cannot close output site file.",5) # vim:fo=trocq:noet:ts=4:sw=4:cin: