-
Notifications
You must be signed in to change notification settings - Fork 66
/
shpadd.c
146 lines (126 loc) · 4.54 KB
/
shpadd.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
/******************************************************************************
*
* Project: Shapelib
* Purpose: Sample application for adding a shape to a shapefile.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
*
* SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
******************************************************************************
*
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shapefil.h"
int main(int argc, char **argv)
{
/* -------------------------------------------------------------------- */
/* Display a usage message. */
/* -------------------------------------------------------------------- */
if (argc < 2)
{
printf("shpadd shp_file [[x y] [+]]*\n");
printf(" or\n");
printf("shpadd shp_file -m [[x y m] [+]]*\n");
printf(" or\n");
printf("shpadd shp_file -z [[x y z] [+]]*\n");
printf(" or\n");
printf("shpadd shp_file -zm [[x y z m] [+]]*\n");
exit(1);
}
const char *filename = argv[1];
argv++;
argc--;
/* -------------------------------------------------------------------- */
/* Check for tuple description options. */
/* -------------------------------------------------------------------- */
const char *tuple = "";
if (argc > 1 && (strcmp(argv[1], "-z") == 0 || strcmp(argv[1], "-m") == 0 ||
strcmp(argv[1], "-zm") == 0))
{
tuple = argv[1] + 1;
argv++;
argc--;
}
/* -------------------------------------------------------------------- */
/* Open the passed shapefile. */
/* -------------------------------------------------------------------- */
SHPHandle hSHP = SHPOpen(filename, "r+b");
if (hSHP == NULL)
{
printf("Unable to open:%s\n", filename);
exit(1);
}
int nShapeType;
SHPGetInfo(hSHP, NULL, &nShapeType, NULL, NULL);
if (argc == 1)
nShapeType = SHPT_NULL;
/* -------------------------------------------------------------------- */
/* Build a vertex/part list from the command line arguments. */
/* -------------------------------------------------------------------- */
int nVMax = 1000;
double *padfX = (double *)malloc(sizeof(double) * nVMax);
double *padfY = (double *)malloc(sizeof(double) * nVMax);
double *padfZ = NULL;
if (strchr(tuple, 'z'))
padfZ = (double *)malloc(sizeof(double) * nVMax);
double *padfM = NULL;
if (strchr(tuple, 'm'))
padfM = (double *)malloc(sizeof(double) * nVMax);
int *panParts;
if ((panParts = (int *)malloc(sizeof(int) * 1000)) == NULL)
{
printf("Out of memory\n");
exit(1);
}
int nParts = 1;
panParts[0] = 0;
int nVertices = 0;
for (int i = 1; i < argc;)
{
if (argv[i][0] == '+')
{
panParts[nParts++] = nVertices;
i++;
}
else if (i < argc - 1 - (int)strlen(tuple))
{
if (nVertices == nVMax)
{
nVMax = nVMax * 2;
padfX = (double *)realloc(padfX, sizeof(double) * nVMax);
padfY = (double *)realloc(padfY, sizeof(double) * nVMax);
if (padfZ)
padfZ = (double *)realloc(padfZ, sizeof(double) * nVMax);
if (padfM)
padfM = (double *)realloc(padfM, sizeof(double) * nVMax);
}
sscanf(argv[i++], "%lg", padfX + nVertices);
sscanf(argv[i++], "%lg", padfY + nVertices);
if (padfZ)
sscanf(argv[i++], "%lg", padfZ + nVertices);
if (padfM)
sscanf(argv[i++], "%lg", padfM + nVertices);
nVertices += 1;
}
}
/* -------------------------------------------------------------------- */
/* Write the new entity to the shape file. */
/* -------------------------------------------------------------------- */
SHPObject *psObject =
SHPCreateObject(nShapeType, -1, nParts, panParts, NULL, nVertices,
padfX, padfY, padfZ, padfM);
SHPWriteObject(hSHP, -1, psObject);
SHPDestroyObject(psObject);
SHPClose(hSHP);
free(panParts);
free(padfX);
free(padfY);
free(padfZ);
free(padfM);
return 0;
}