Skip to content

One-to-many table relationship to shapefile polygon in spplot / plot - possible?

6 messages · Roger Bivand, Barry Rowlingson, Noli Sicad

#
Hi,

I have problem transposing my database table which is to be merged /
joined with the shapefile file (*.dbf) inorder to have one-to-one
relationship with shapefile polygon. As we know that inorder for the
polygon to be rendered in GIS, it has to have one-to-ope relationship.

One to many table:

Poly_ID Period Age
01           1       32
01           2       34
02           1       30
03           2       21


One to one table

Poly_ID Period1 Period2 Period3
01            32           34
02             30
03                           21
04                                    35

Now,  is it possible to use one-to-many table for mapping using spplot / plot?

The result data I am trying to map using spplot has 1 to 70 years
periods. I want to produce maps per period, for 10 years or more for
forest management regimes in forest stand.

Any alternative?

Thanks in advance.

Noli
#
On Wed, 6 Jan 2010, Noli Sicad wrote:

            
Review ?reshape to convert to wide format before trying to merge into 
your object for using spplot().

Roger

  
    
#
OK. The answer is not possible.

I have been studying reshape - cast and melt.  The illustration submit
in my earlier post, can be easily done. But here is the real data
(below) is bit harder.

I have more problem deleting shapefile polygons that doesn't have data
in dbf.  I am just learning to use R in the last weeks. I have to find
some example script how to do polygon delete. I am reading sp package.
Searching in the archives of various list.

Thanks, Noli
~~~~~~~~~


Input data

C_ID    CROP  Period    Age
83      SORI    1       31
84      SORI    1       32
85      SORI    1       33
86      SORI          1        34
82      SORI       2            28
83      SORI        2   29
84      SORI       2    30
93      SORM    2       35
62      OTRM    3       30
82      SORI        3      27
3       SORM    3       35
82      SORI    4       26
4       SORM    4       34
5       OTRI           5                25
5       OTRM    5       29
5       SORM    5       32
5       SORM    5       33
5       SORM    5       35
6       OTRI    6       22
6       OTRI         6       23
6       OTRI       6        24
6       OTRM    6       26
6       OTRM    6       27
6       OTRM    6       28
7       OTRM    7       26
8       OTRM    8       26
9       OTRM    9       26
10      OTRM    10      24
10      OTRM    10      25
10      OTRM    10      26
10      SORM    10      31

Desired table
C_ID    CROP    P 1     P 2     P 3     P 4     P 5     P 6     P 7
 P 8     P 9     P 10
- Hide quoted text -
83      SORI      31
84      SORI      32
85      SORI      33
86      SORI      34
82      SORI            28
83      SORI            29
84      SORI            30
93      SORM            35
62      OTRM            30
82      SORI                    27
3       SORM                    35
82      SORI                            26
4       SORM                            34
5       OTRI                                    25
5       OTRM                                    29
5       SORM                                    32
5       SORM                                    33
5       SORM                                    35
6       OTRI                                            22
6       OTRI                                            23
6       OTRI                                            24
6       OTRM                                            26
6       OTRM                                            27
6       OTRM                                            28
7       OTRM                                                    26
8       OTRM                                                            26
9       OTRM
         26
10      OTRM
                 24
10      OTRM
                 25
10      OTRM
                 26
10      SORM
                 31
#
On Wed, 6 Jan 2010, Noli Sicad wrote:

            
If you start with reshape in the stats package, not the reshape package, 
it may be easier. Using reshape is always hard, but the wide format is 
what you need. There is no provision for one-to-many for spatial features.
You delete spatial features from a Spatial* or Spatial*DataFrame object in 
the usual way with the "[" operator. Just treat them as data.frame objects 
as a first approximation.

Roger

  
    
#
On Wed, Jan 6, 2010 at 6:16 AM, Noli Sicad <nsicad at gmail.com> wrote:
Depends on your GIS! Shapefiles only have a 1-1 relationship, but
that's because they are a bit rubbish. If you use a proper spatial
database then you can do one-many. This is an application for a
PostGIS or Spatialite database, and if your GIS can read that (Qgis
for example) then you are sorted.
I wouldn't reshape the data in R to create a possibly sparse matrix,
and then have to code in column names with expressions (e.g.
data[,paste("Year",y,sep="")], which I think is icky). I would just
create a spatial polygons/points data set with just an ID column, and
put your ID/Period/Age data in a non-spatial data frame. Then when you
want to plot a particular thing, just match up the data with the
spatial data and plot (using the 'match' function).

Barry
#
I want to do it in R. R can do quick and pretty graphs and maps!
This is what actually I wanted!

"column names with expressions (e.g. data[,paste("Year",y,sep="")]" in
shapefile database?

Yes, created Crop_ID which is supposed to be Polygon ID in my
foreststand.dbf. I have also Crop_ID, Period, Age in non spatial table
i.e. harvest.dbf

This is the harvest.dbf
names(harvest.dat) = c("CROP_ID", "CROPTYPE",
"Period","Ini_Age","Cut_Age", "Area_Cut")

Barry, any snippet how to use match in spplot or plot.

Thanks. Noli