[GRASSLIST:151] Re: s.label -- MIA?

Matej Cepl cepl at surfbest.net
Fri May 23 12:53:24 EDT 2003


On Fri, May 23, 2003 at 05:48:23PM +1200, H Bowman wrote:
>s.label isn't there, see:
>http://article.gmane.org/gmane.comp.gis.grass.user/611

OK, I have got the message. Attached are two scripts (one is the 
previous version slightly debugged, and other creates directly 
label file). Is there any documentation on the proper format of 
label files?

Matej

-- 
Matej Cepl, http://www.ceplovi.cz/matej
GPG Finger: 89EF 4BC6 288A BF43 1BAB  25C3 E09F EF25 D964 84AC
138 Highland Ave. #10, Somerville, Ma 02143, (617) 623-1488
 
I went to a Grateful Dead Concert and they played for SEVEN
hours.  Great song.
    -- Fred Reuss

-------------- next part --------------
#!/usr/bin/env python
"""
v.to.site.area -- export area label points and category labels 
into site.

Copyright (c) 2003 Matej Cepl <matej at ceplovi.cz>
   
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.4 2003/05/23 16:46:01 matej Exp $
"""

from sys import exit,argv,stderr
from os import environ,popen,sep
from os.path import abspath,exists
from string import split

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=split(incat[catno])
	if line[0]=="A":
		cats[str(line[3])]=line[1],line[2]

# Prepare reading category label file
line=split(inlab[0])
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=split(inlab[labno],':')
	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:
-------------- next part --------------
#!/usr/bin/env python
"""
v.to.site.area -- export area label points and category labels 
into site.

Copyright (c) 2003 Matej Cepl <matej at ceplovi.cz>
   
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.label.area,v 1.2 2003/05/23 16:30:27 matej Exp matej $
"""

from sys import exit,argv,stderr
from os import environ,popen,sep,mkdir
from os.path import abspath,exists
from string import split

commonoutstuff="""
xoffset:
yoffset:
ref:                 center
font:                standard
color:               brown
size:                20
width:               2
hcolor:              none
hwidth:              0
background:          none
border:              none
opaque:              yes
"""

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+'/labels/')):
	mkdir(location+'/labels/')

outfilename=abspath(location+'/labels/'+inmap)
try:
	outfile=open(outfilename,'w')
except:
	errorexit("Cannot open output site file.",5)

cats={}
# east,north
for catno in range(len(incat)):
	line=split(incat[catno])
	if line[0]=="A":
		cats[str(line[3])]=line[1],line[2]

# Prepare reading category label file
line=split(inlab[0])
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
for labno in range(4,len(inlab)):
	line=split(inlab[labno],':')
	id=str(line[0])
	label=str(line[1])
	outfile.write("east:\t"+cats[id][0]+"\n")
	outfile.write("north:\t"+cats[id][1])
	outfile.write(commonoutstuff+"\n")
	outfile.write("text:\t"+str(line[1])+"\n")

try:
	outfile.close()
except:
	errorexit("Cannot close output site file.",5)

# vim:fo=trocq:noet:ts=4:sw=4:cin:


More information about the grass-user mailing list