Vector length of common boundaries: Difference between revisions

From GRASS-Wiki
Jump to navigation Jump to search
(new, from http://www.mail-archive.com/r-sig-geo@stat.math.ethz.ch/msg00099.html)
 
(Update url of SIDS SHP file)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Q:''' Any way to get the length of common boundaries shared by neighbouring areas in a map?
'''Q:''' Any way to get the length of common boundaries shared by neighbouring areas in a map?


'''A:''''  
'''A:'''


Sample data set: [http://r-spatial.sourceforge.net/gallery/ sudden infant deaths data] from North Carolina, data imported from SHAPE file with {{cmd|v.in.ogr}}.
Sample data set: [https://geodacenter.github.io/data-and-lab/data/sids.zip sudden infant deaths data (SHP file)] from North Carolina ([https://r-spatial.github.io/spdep/articles/sids.html source1], [https://geodacenter.github.io/data-and-lab/ source2]); data imported from SHAPE file with {{cmd|v.in.ogr}}.


Let's have a look at the data:
Let's have a look at the data:
  d.mon x0
  d.mon wx0
  d.vect sids_nc
  d.vect sids_nc


Line 18: Line 18:
We want only one category per boundary, that's why the sides check is needed (a boundary may consist of several pieces)
We want only one category per boundary, that's why the sides check is needed (a boundary may consist of several pieces)
So we create a new attribute table and link it to the new layer 2 of the vector map:
So we create a new attribute table and link it to the new layer 2 of the vector map:
  v.db.addtable sids_nc2 layer=2 col="left integer,right integer,length integer"
  {{cmd|v.db.addtable}} sids_nc2 layer=2 col="left integer,right integer,length integer"


Now we query the polygon/boundary relationships and store it into the attribute table linked to layer 2:
Now we query the polygon/boundary relationships and store it into the attribute table linked to layer 2:
  v.to.db map=sids_nc2 option=sides col=left,right layer=2  
  {{cmd|v.to.db}} map=sids_nc2 option=sides col=left,right layer=2  


Now we have unique categories for the boundaries and can calculate the lengths:
Now we have unique categories for the boundaries and can calculate the lengths:
Line 29: Line 29:


See the new attribute table containing the boundary lengths:
See the new attribute table containing the boundary lengths:
  v.db.select sids_nc2 layer=2
  {{cmd|v.db.select}} sids_nc2 layer=2


Verification (let's check boundary #193):
Verification (let's check boundary #193):
  d.vect sids_nc2 cat=193 layer=2 col=red type=boundary
  {{cmd|d.vect}} sids_nc2 cat=193 layer=2 col=red type=boundary
  d.zoom
  {{cmd|d.zoom}}
  d.measure
  {{cmd|d.measure}}
  # LEN:    12756.00 meters
  # LEN:    12756.00 meters


What does the attribute table say:
What does the attribute table say:
  v.db.select sids_nc2 layer=2 | grep '^193'
  {{cmd|v.db.select}} sids_nc2 layer=2 | grep '^193'
  190|65|68|12814
  190|65|68|12814


Line 44: Line 44:


[[Category: FAQ]]
[[Category: FAQ]]
[[Category:Vector]]

Latest revision as of 20:10, 16 November 2020

Q: Any way to get the length of common boundaries shared by neighbouring areas in a map?

A:

Sample data set: sudden infant deaths data (SHP file) from North Carolina (source1, source2); data imported from SHAPE file with v.in.ogr.

Let's have a look at the data:

d.mon wx0
d.vect sids_nc

We add a second layer to the map which references the boundaries of polygons. In the vector geometry we generate an ID (category) for each boundary:

v.category sids_nc out=sids_nc2 layer=2 type=boundary option=add

Underlying idea: we'll fetch the IDs (categories) of the polygons left and right from each boundary and store it into the attribute table linked to layer 2. In general:

# cat_of_boundary | cat_of_left_polygon | cat_of_right_polygon | 
length_of_boundary

We want only one category per boundary, that's why the sides check is needed (a boundary may consist of several pieces) So we create a new attribute table and link it to the new layer 2 of the vector map:

v.db.addtable sids_nc2 layer=2 col="left integer,right integer,length integer"

Now we query the polygon/boundary relationships and store it into the attribute table linked to layer 2:

v.to.db map=sids_nc2 option=sides col=left,right layer=2 

Now we have unique categories for the boundaries and can calculate the lengths:

v.to.db map=sids_nc2 option=length col=length layer=2 

Done.

See the new attribute table containing the boundary lengths:

v.db.select sids_nc2 layer=2

Verification (let's check boundary #193):

d.vect sids_nc2 cat=193 layer=2 col=red type=boundary
d.zoom
d.measure
# LEN:     12756.00 meters

What does the attribute table say:

v.db.select sids_nc2 layer=2 | grep '^193'
190|65|68|12814

This is reasonably close since on screen digitization in d.measure isn't always that precise ...