[GRASS-dev] easy way to load x, y (of centroid) and top, bottom, left, right coordinates to attribute table?

hi,

is there an easy way to load x,y (of centroid) and top, bottom, left, right
coordinates of every geometry (polygon, line) to the vector attribute table?

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/easy-way-to-load-x-y-of-centroid-and-top-bottom-left-right-coordinates-to-attribute-table-tp5305135.html
Sent from the Grass - Dev mailing list archive at Nabble.com.

On 27/01/17 21:35, Helmut Kudrnovsky wrote:

hi,

is there an easy way to load x,y (of centroid) and top, bottom, left, right
coordinates of every geometry (polygon, line) to the vector attribute table?

For the centroid coordinates:

v.db.addcolumn YourMap column="x double precision, y double precision"
v.to.db YourMap op=coor col=x,y

For the bounding box coordinates, I don't know any easy way. Would be a nice thing to have in v.to.db (see [1]).

Moritz

[1] http://trac.osgeo.org/grass/ticket/2122

For the centroid coordinates:

v.db.addcolumn YourMap column="x double precision, y double precision"
v.to.db YourMap op=coor col=x,y

just tried with some vectors in a wgs84 ll location:

v.report map=testcalculate option=coor
cat|id|x|y|l|r|t|b|x2|y2|x|y|z
1||9.8489673562|47.0568937369|9.0801356183|10.6452349706|47.6779179411|46.4101061965|9.85074274819765|47.0584209060437|9.85074274819765|47.0584209060437|0.0
2||11.8802743895|46.9879350674|11.0347074901|12.7965928607|47.8068606443|46.1987432153|11.8761701610656|46.990400528994|11.8761701610656|46.990400528994|0.0
3||9.7422504597|46.163754262|9.0475885875|10.3944050709|46.4383355922|45.9348643802|9.737413876863|46.16534963624|9.737413876863|46.16534963624|0.0

x and y are the centroid coordinates calculated by QGIS
x2 and y2 are the centroi coordinates calculated by v.to.db
and the last (identical to x2 and y2) reported by v.report

it's interesting that QGIS and GRASS calculates differnt centroid
coordinates.

any idea?

For the bounding box coordinates, I don't know any easy way. Would be a
nice thing to have in v.to.db (see [1]).

yes indeed, would be a nice addition to v.to.db. updated the ticket.

taken from a little script coded some time ago:

    ######
    # print bbox of vector layer geometries
    ######
    if print_bbox_vector_geometries :
    # open vector
    vtopo = VectorTopo(vector)
    vtopo.open(mode = 'r')
    # loop thru vector geometries
    for i in range(1, vcats_max_plus):
        # get vector geometries area info
        vect_geom_area = Area(v_id = i, c_mapinfo = vtopo.c_mapinfo)
        # get vector geometries bbox info
        vect_geom_bb = vect_geom_area.bbox()
        # bbox north, south, west, east
        bb_north = vect_geom_bb.north
        bb_south = vect_geom_bb.south
        bb_west = vect_geom_bb.west
        bb_east = vect_geom_bb.east
        grass.message("cat %s: north: %s south: %s west: %s east: %s" %(i,
bb_north, bb_south, bb_west, bb_east))
    # close vector
vtopo.close()

so it's already in the vector data available, would be nice to have it in
v.to.db.

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/easy-way-to-load-x-y-of-centroid-and-top-bottom-left-right-coordinates-to-attribute-table-tp5305135p5305175.html
Sent from the Grass - Dev mailing list archive at Nabble.com.

On 28/01/17 12:19, Helmut Kudrnovsky wrote:

For the centroid coordinates:

v.db.addcolumn YourMap column="x double precision, y double precision"
v.to.db YourMap op=coor col=x,y

just tried with some vectors in a wgs84 ll location:

v.report map=testcalculate option=coor
cat|id|x|y|l|r|t|b|x2|y2|x|y|z
1||9.8489673562|47.0568937369|9.0801356183|10.6452349706|47.6779179411|46.4101061965|9.85074274819765|47.0584209060437|9.85074274819765|47.0584209060437|0.0
2||11.8802743895|46.9879350674|11.0347074901|12.7965928607|47.8068606443|46.1987432153|11.8761701610656|46.990400528994|11.8761701610656|46.990400528994|0.0
3||9.7422504597|46.163754262|9.0475885875|10.3944050709|46.4383355922|45.9348643802|9.737413876863|46.16534963624|9.737413876863|46.16534963624|0.0

x and y are the centroid coordinates calculated by QGIS
x2 and y2 are the centroi coordinates calculated by v.to.db
and the last (identical to x2 and y2) reported by v.report

it's interesting that QGIS and GRASS calculates differnt centroid
coordinates.

any idea?

I don't know which tool you used in QGIS, but GRASS' centroids are not guaranteed to be the geometrical centroid (aka "center of gravity" which could sometimes fall outside the polygon). You might want to compare the results of QGIS with those of the addon v.centerpoint.

For the bounding box coordinates, I don't know any easy way. Would be a
nice thing to have in v.to.db (see [1]).

yes indeed, would be a nice addition to v.to.db. updated the ticket.

taken from a little script coded some time ago:

    ######
    # print bbox of vector layer geometries
    ######
    if print_bbox_vector_geometries :
    # open vector
    vtopo = VectorTopo(vector)
    vtopo.open(mode = 'r')
    # loop thru vector geometries
    for i in range(1, vcats_max_plus):
        # get vector geometries area info
        vect_geom_area = Area(v_id = i, c_mapinfo = vtopo.c_mapinfo)
        # get vector geometries bbox info
        vect_geom_bb = vect_geom_area.bbox()
        # bbox north, south, west, east
        bb_north = vect_geom_bb.north
        bb_south = vect_geom_bb.south
        bb_west = vect_geom_bb.west
        bb_east = vect_geom_bb.east
        grass.message("cat %s: north: %s south: %s west: %s east: %s" %(i,
bb_north, bb_south, bb_west, bb_east))
    # close vector
vtopo.close()

so it's already in the vector data available, would be nice to have it in
v.to.db.

Yes, there's Vect_get_area_box in the Vlib (box.c), so it shouldn't be too hard to integrate it.

Moritz

I don't know which tool you used in QGIS,

it's quite easy in QGIS, just open the attribute table for editing and type

x (centroid( $geometry ))
y (centroid( $geometry ))

in the calculator for calculating the centroid coordinates.

but GRASS' centroids are not
guaranteed to be the geometrical centroid (aka "center of gravity" which
could sometimes fall outside the polygon).

ah yes, you're right.

You might want to compare the
results of QGIS with those of the addon v.centerpoint.

v.centerpoint input=testcalculate output=testcalculate_center_of_gravity

v.db.select map=testcalculate_center_of_gravity columns=x,y,x3,y3
x|y|x3|y3
9.8489673562|47.0568937369|9.8489673562097|47.0568937369153
11.8802743895|46.9879350674|11.880274389498|46.9879350674375
9.7422504597|46.163754262|9.74225045972473|46.1637542620065

yes, GRASS' "center of gravity" (x3,y3) and QGIS centroids (x,y) are the
same.

thanks

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/easy-way-to-load-x-y-of-centroid-and-top-bottom-left-right-coordinates-to-attribute-table-tp5305135p5305180.html
Sent from the Grass - Dev mailing list archive at Nabble.com.