[GRASS-dev] problem with parse_command python
Luca Delucchi
lucadeluge at gmail.com
Mon Dec 27 11:29:24 EST 2010
2010/12/23 Glynn Clements <glynn at gclements.plus.com>:
>
Hi Glynn, thanks for the answer
> parse_key_val returns a dictionary, so the keys must be unique. If you
> have duplicate keys, only the last value will be returned.
>
> For v.what, you'll need to parse the output yourself.
>
ok, I understood it
> For the future, we should consider fixing v.what to use a sane (i.e.
> non-modal) output format for -g.
>
I try to create a general function for v.what, it return a dictionary
of dictionary
def vector_what(map,x,y):
"""!Return the result of v.what using the map and coordinates passed
@param map vector map name
@param x x coordinate
@param y y coordinate
@return parsed output in dictionary
"""
result = {}
# create string for east_north param
coor = str(x) + ',' + str(y)
fields = grass.read_command('v.what',flags = 'a', map = map,
east_north = coor)
# split the different features
fields = fields.split('\n\n')
# value for number of features
value = 0
# delete the first block, is only the coordinates to query
del fields[0]
# for each block
for field in fields:
# create a temporary dictionary
temp_dic = {}
# for each block split lines
lines = field.splitlines()
# for each line
for line in lines:
kv = line.split(':', 1)
if len(kv) > 1:
temp_dic[kv[0].strip()] = str(kv[1])
# this is the firs block it contain general information about map
if value == 0:
result['general'] = temp_dic
# features
else:
result['feature'+str(value)] = temp_dic
value = value + 1
return result
what do you think? maybe it isn't the best solution but it work fine...
the code is available to be integrated into vector.py
> --
> Glynn Clements <glynn at gclements.plus.com>
>
best regards
Luca
More information about the grass-dev
mailing list