forked from OSGeo/shapelib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shptest.c
253 lines (214 loc) · 7.43 KB
/
shptest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/******************************************************************************
*
* Project: Shapelib
* Purpose: Application for generating sample Shapefiles of various types.
* Used by the stream2.sh test script.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
*
* SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
******************************************************************************
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shapefil.h"
/************************************************************************/
/* Test_WritePoints() */
/* */
/* Write a small point file. */
/************************************************************************/
static void Test_WritePoints(int nSHPType, const char *pszFilename)
{
SHPHandle hSHPHandle = SHPCreate(pszFilename, nSHPType);
double x = 1.0;
double y = 2.0;
double z = 3.0;
double m = 4.0;
SHPObject *psShape =
SHPCreateObject(nSHPType, -1, 0, NULL, NULL, 1, &x, &y, &z, &m);
SHPWriteObject(hSHPHandle, -1, psShape);
SHPDestroyObject(psShape);
x = 10.0;
y = 20.0;
z = 30.0;
m = 40.0;
psShape = SHPCreateObject(nSHPType, -1, 0, NULL, NULL, 1, &x, &y, &z, &m);
SHPWriteObject(hSHPHandle, -1, psShape);
SHPDestroyObject(psShape);
SHPClose(hSHPHandle);
}
/************************************************************************/
/* Test_WriteMultiPoints() */
/* */
/* Write a small multipoint file. */
/************************************************************************/
static void Test_WriteMultiPoints(int nSHPType, const char *pszFilename)
{
double x[4];
double y[4];
double z[4];
double m[4];
SHPHandle hSHPHandle = SHPCreate(pszFilename, nSHPType);
for (int iShape = 0; iShape < 3; iShape++)
{
for (int i = 0; i < 4; i++)
{
x[i] = iShape * 10 + i + 1.15;
y[i] = iShape * 10 + i + 2.25;
z[i] = iShape * 10 + i + 3.35;
m[i] = iShape * 10 + i + 4.45;
}
SHPObject *psShape =
SHPCreateObject(nSHPType, -1, 0, NULL, NULL, 4, x, y, z, m);
SHPWriteObject(hSHPHandle, -1, psShape);
SHPDestroyObject(psShape);
}
SHPClose(hSHPHandle);
}
/************************************************************************/
/* Test_WriteArcPoly() */
/* */
/* Write a small arc or polygon file. */
/************************************************************************/
static void Test_WriteArcPoly(int nSHPType, const char *pszFilename)
{
SHPHandle hSHPHandle = SHPCreate(pszFilename, nSHPType);
int anPartType[100];
int *panPartType;
if (nSHPType == SHPT_MULTIPATCH)
panPartType = anPartType;
else
panPartType = NULL;
double x[100];
double y[100];
double z[100];
double m[100];
for (int iShape = 0; iShape < 3; iShape++)
{
x[0] = 1.0;
y[0] = 1.0 + iShape * 3;
x[1] = 1.0;
y[1] = 2.0 + iShape * 3;
x[2] = 2.0;
y[2] = 2.0 + iShape * 3;
x[3] = 2.0;
y[3] = 1.0 + iShape * 3;
x[4] = 1.0;
y[4] = 1.0 + iShape * 3;
for (int i = 0; i < 5; i++)
{
z[i] = iShape * 10 + i + 3.35;
m[i] = iShape * 10 + i + 4.45;
}
SHPObject *psShape =
SHPCreateObject(nSHPType, -1, 0, NULL, NULL, 5, x, y, z, m);
SHPWriteObject(hSHPHandle, -1, psShape);
SHPDestroyObject(psShape);
}
/* -------------------------------------------------------------------- */
/* Do a multi part polygon (shape). We close it, and have two */
/* inner rings. */
/* -------------------------------------------------------------------- */
x[0] = 0.0;
y[0] = 0.0;
x[1] = 0;
y[1] = 100;
x[2] = 100;
y[2] = 100;
x[3] = 100;
y[3] = 0;
x[4] = 0;
y[4] = 0;
x[5] = 10;
y[5] = 20;
x[6] = 30;
y[6] = 20;
x[7] = 30;
y[7] = 40;
x[8] = 10;
y[8] = 40;
x[9] = 10;
y[9] = 20;
x[10] = 60;
y[10] = 20;
x[11] = 90;
y[11] = 20;
x[12] = 90;
y[12] = 40;
x[13] = 60;
y[13] = 40;
x[14] = 60;
y[14] = 20;
for (int i = 0; i < 15; i++)
{
z[i] = i;
m[i] = i * 2;
}
int anPartStart[100];
anPartStart[0] = 0;
anPartStart[1] = 5;
anPartStart[2] = 10;
anPartType[0] = SHPP_RING;
anPartType[1] = SHPP_INNERRING;
anPartType[2] = SHPP_INNERRING;
SHPObject *psShape = SHPCreateObject(nSHPType, -1, 3, anPartStart,
panPartType, 15, x, y, z, m);
SHPWriteObject(hSHPHandle, -1, psShape);
SHPDestroyObject(psShape);
SHPClose(hSHPHandle);
}
/************************************************************************/
/* main() */
/************************************************************************/
int main(int argc, char **argv)
{
/* -------------------------------------------------------------------- */
/* Display a usage message. */
/* -------------------------------------------------------------------- */
if (argc != 2)
{
printf("shptest test_number\n");
exit(1);
}
/* -------------------------------------------------------------------- */
/* Figure out which test to run. */
/* -------------------------------------------------------------------- */
if (atoi(argv[1]) == 0)
Test_WritePoints(SHPT_NULL, "test0.shp");
else if (atoi(argv[1]) == 1)
Test_WritePoints(SHPT_POINT, "test1.shp");
else if (atoi(argv[1]) == 2)
Test_WritePoints(SHPT_POINTZ, "test2.shp");
else if (atoi(argv[1]) == 3)
Test_WritePoints(SHPT_POINTM, "test3.shp");
else if (atoi(argv[1]) == 4)
Test_WriteMultiPoints(SHPT_MULTIPOINT, "test4.shp");
else if (atoi(argv[1]) == 5)
Test_WriteMultiPoints(SHPT_MULTIPOINTZ, "test5.shp");
else if (atoi(argv[1]) == 6)
Test_WriteMultiPoints(SHPT_MULTIPOINTM, "test6.shp");
else if (atoi(argv[1]) == 7)
Test_WriteArcPoly(SHPT_ARC, "test7.shp");
else if (atoi(argv[1]) == 8)
Test_WriteArcPoly(SHPT_ARCZ, "test8.shp");
else if (atoi(argv[1]) == 9)
Test_WriteArcPoly(SHPT_ARCM, "test9.shp");
else if (atoi(argv[1]) == 10)
Test_WriteArcPoly(SHPT_POLYGON, "test10.shp");
else if (atoi(argv[1]) == 11)
Test_WriteArcPoly(SHPT_POLYGONZ, "test11.shp");
else if (atoi(argv[1]) == 12)
Test_WriteArcPoly(SHPT_POLYGONM, "test12.shp");
else if (atoi(argv[1]) == 13)
Test_WriteArcPoly(SHPT_MULTIPATCH, "test13.shp");
else
{
printf("Test `%s' not recognised.\n", argv[1]);
exit(10);
}
exit(0);
}