1 # include "rgdal.h"
2 
make_Polygonlist(SEXP iG,SEXP iGc)3 SEXP make_Polygonlist(SEXP iG, SEXP iGc) {
4 
5     SEXP res, jG, jGc;
6     int n, i, pc=0;
7 
8     n = length(iG);
9 
10     PROTECT(res = NEW_LIST(n)); pc++;
11     PROTECT(jGc = NEW_INTEGER(1)); pc++;
12 
13     for (i=0; i<n; i++) {
14 
15         jG = VECTOR_ELT(iG, i);
16 // hole setting based on comments by default (OGC SFS order)
17 // but by ring order if comment NULL 121019
18         if (iGc == R_NilValue) {
19             SET_VECTOR_ELT(res, i, make_Polygon(jG, R_NilValue));
20         } else {
21             INTEGER_POINTER(jGc)[0] = INTEGER_POINTER(iGc)[i];
22             SET_VECTOR_ELT(res, i, make_Polygon(jG, jGc));
23         }
24     }
25 
26     UNPROTECT(pc);
27     return(res);
28 
29 }
30 
make_Polygon(SEXP jG,SEXP jGc)31 SEXP make_Polygon(SEXP jG, SEXP jGc) {
32 
33     SEXP res, coords, dim, dimnames, n, ihole;
34     int i, nn, pc=0, copy1=FALSE;
35 
36     nn = length(VECTOR_ELT(jG, 0));
37     if ((NUMERIC_POINTER(VECTOR_ELT(jG, 0))[0] !=
38          NUMERIC_POINTER(VECTOR_ELT(jG, 0))[(nn-1)]) &&
39         (NUMERIC_POINTER(VECTOR_ELT(jG, 1))[0] !=
40          NUMERIC_POINTER(VECTOR_ELT(jG, 1))[(nn-1)])) copy1 = TRUE;
41     if (copy1) {
42 
43         PROTECT(coords = NEW_NUMERIC((nn+1)*2)); pc++;
44 
45         for (i=0; i<nn; i++) {
46             NUMERIC_POINTER(coords)[i] = NUMERIC_POINTER(VECTOR_ELT(jG, 0))[i];
47             NUMERIC_POINTER(coords)[i+(nn+1)] =
48                 NUMERIC_POINTER(VECTOR_ELT(jG, 1))[i];
49         }
50         NUMERIC_POINTER(coords)[nn] =
51             NUMERIC_POINTER(VECTOR_ELT(jG, 0))[0];
52         NUMERIC_POINTER(coords)[nn+(nn+1)] =
53             NUMERIC_POINTER(VECTOR_ELT(jG, 1))[0];
54 
55     } else {
56 
57         PROTECT(coords = NEW_NUMERIC(nn*2)); pc++;
58 
59         for (i=0; i<nn; i++) {
60             NUMERIC_POINTER(coords)[i] = NUMERIC_POINTER(VECTOR_ELT(jG, 0))[i];
61             NUMERIC_POINTER(coords)[i+nn] =
62                 NUMERIC_POINTER(VECTOR_ELT(jG, 1))[i];
63         }
64     }
65 
66 // hole setting based on comments by default (OGC SFS order)
67 // but by ring order if comment NULL 121019
68     PROTECT(ihole = NEW_INTEGER(1)); pc++;
69     INTEGER_POINTER(ihole)[0] = 0L;
70     if (jGc == R_NilValue) INTEGER_POINTER(ihole)[0] = NA_INTEGER;
71     else if (INTEGER_POINTER(jGc)[0] != 0L) INTEGER_POINTER(ihole)[0] = 1L;
72     PROTECT(n = NEW_INTEGER(1)); pc++;
73     if (copy1) INTEGER_POINTER(n)[0] = nn+1;
74     else INTEGER_POINTER(n)[0] = nn;
75 
76     PROTECT(dim = NEW_INTEGER(2)); pc++;
77     INTEGER_POINTER(dim)[0] = (int) INTEGER_POINTER(n)[0];
78     INTEGER_POINTER(dim)[1] = (int) 2;
79     setAttrib(coords, R_DimSymbol, dim);
80     PROTECT(dimnames = NEW_LIST(2)); pc++;
81     SET_VECTOR_ELT(dimnames, 1, NEW_CHARACTER(2));
82     SET_STRING_ELT(VECTOR_ELT(dimnames, 1), 0, COPY_TO_USER_STRING("x"));
83     SET_STRING_ELT(VECTOR_ELT(dimnames, 1), 1, COPY_TO_USER_STRING("y"));
84 
85     PROTECT(res = SP_PREFIX(Polygon_c)(coords, n, ihole)); pc++;
86 
87     UNPROTECT(pc);
88     return(res);
89 
90 }
91 
92 
rgdal_sp_linkingTo_version(void)93 SEXP rgdal_sp_linkingTo_version(void) {
94     return(SP_PREFIX(sp_linkingTo_version)());
95 }
96 
97