[GRASS-dev] problem with parse_command python
Luca Delucchi
lucadeluge at gmail.com
Wed Dec 29 08:32:08 EST 2010
2010/12/29 Glynn Clements <glynn at gclements.plus.com>:
>> # create string for east_north param
>> coor = str(x) + ',' + str(y)
>
> This isn't necessary; you can pass numbers, lists and tuples to the
> grass.*_command() functions and they will be converted automatically.
>
ok, I change them with a list
>> fields = grass.read_command('v.what',flags = 'a', map = map, east_north = coor)
>
> I suggest using the -g flag.
>
ok, but read down....
>> # split the different features
>> fields = fields.split('\n\n')
>
> This isn't correct. Without -g, the blank line occurs betwen the
> Layer: and Category: lines and the rest of the per-layer information.
I don't know if this is correct, look the answer of "fields =
grass.read_command('v.what',flags = 'a', map = map_tiles, east_north =
pointslist)"
print fields
East: 636280
North: 5079748
Map: test_tile
Mapset: ortofoto_baldo
Type: Area
Sq Meters: 770560.000
Hectares: 77.056
Acres: 190.410
Sq Miles: 0.2975
Layer: 1
Category: 1
Driver: dbf
Database: /grassdata/patUTM32/ortofoto_baldo/dbf/
Table: test_tile
Key column: cat
cat : 1
location : /grassdata/patUTM32/ortofoto_baldo/cellhd/22_1683_tagliata
Layer: 1
Category: 13
Driver: dbf
Database: /grassdata/patUTM32/ortofoto_baldo/dbf/
Table: test_tile
Key column: cat
cat : 13
location : /grassdata/patUTM32/ortofoto_baldo/cellhd/24a_3001_tagliata
Layer: 2
Category: 2
I think the first word of new feature is Driver, I wrong?
> You need to scan through the lines and start a new dictionary whenever
> you find a "Layer" line.
>
ok, here the new code....
def vector_what(map,coor):
"""!Return the result of v.what using the map and coordinates passed
@param map vector map name
@param coor a list of x,y coordinates
@return parsed output in dictionary
"""
result = {}
# create string for east_north param
fields = grass.read_command('v.what',flags = 'ag', map = map,
east_north = coor)
#split lines
fields = fields.splitlines()
# value for number of features
value = 0
# create a temporary dictionary
temp_dic = {}
# for each line
for field in fields:
# split key and value
kv = field.split('=', 1)
if len(kv) > 1:
#Start the new feature
if kv[0].strip() == 'Driver':
# if value is 0 dictionary contain general information about map
if value == 0:
result['general'] = temp_dic
# else features
else:
result['feature'+str(value)] = temp_dic
# create a new temporary dictionary
temp_dic = {}
# add driver to the new dictionary
temp_dic[kv[0].strip()] = str(kv[1])
# value for the new feature
value = value + 1
else:
# add value to the dictionary
temp_dic[kv[0].strip()] = str(kv[1])
# add the last feature
result['feature'+str(value)] = temp_dic
return result
> --
> Glynn Clements <glynn at gclements.plus.com>
>
thanks
Luca
www.lucadelu.org
More information about the grass-dev
mailing list