forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshptree.c
156 lines (133 loc) · 5.69 KB
/
shptree.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
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: Commandline utility to generate .qix shapefile spatial indexes.
* Author: Steve Lime and the MapServer team.
*
******************************************************************************
* Copyright (c) 1996-2005 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "mapserver.h"
#include "maptree.h"
#include <string.h>
char* AddFileSuffix ( const char * Filename, const char * Suffix )
{
char *pszFullname, *pszBasename;
int i;
/* -------------------------------------------------------------------- */
/* Compute the base (layer) name. If there is any extension */
/* on the passed in filename we will strip it off. */
/* -------------------------------------------------------------------- */
pszBasename = (char *) msSmallMalloc(strlen(Filename)+5);
strcpy( pszBasename, Filename );
for( i = (int)strlen(pszBasename)-1;
i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
&& pszBasename[i] != '\\';
i-- ) {}
if( pszBasename[i] == '.' )
pszBasename[i] = '\0';
/* -------------------------------------------------------------------- */
/* Open the .shp and .shx files. Note that files pulled from */
/* a PC to Unix with upper case filenames won't work! */
/* -------------------------------------------------------------------- */
pszFullname = (char *) msSmallMalloc(strlen(pszBasename) + 5);
sprintf( pszFullname, "%s%s", pszBasename, Suffix);
free(pszBasename);
return (pszFullname);
}
int main(int argc, char *argv[])
{
shapefileObj shapefile;
treeObj *tree;
int byte_order = MS_NEW_LSB_ORDER, i;
int depth=0;
if(argc > 1 && strcmp(argv[1], "-v") == 0) {
printf("%s\n", msGetVersion());
exit(0);
}
/* -------------------------------------------------------------------- */
/* Establish the byte order on this machine to decide default */
/* index format */
/* -------------------------------------------------------------------- */
i = 1;
/* cppcheck-suppress knownConditionTrueFalse */
if( *((uchar *) &i) == 1 )
byte_order = MS_NEW_LSB_ORDER;
else
byte_order = MS_NEW_MSB_ORDER;
if(argc<2) {
fprintf(stdout,"Syntax:\n");
fprintf(stdout," shptree <shpfile> [<depth>] [<index_format>]\n" );
fprintf(stdout,"Where:\n");
fprintf(stdout," <shpfile> is the name of the .shp file to index.\n");
fprintf(stdout," <depth> (optional) is the maximum depth of the index\n");
fprintf(stdout," to create, default is 0 meaning that shptree\n");
fprintf(stdout," will calculate a reasonable default depth.\n");
fprintf(stdout," <index_format> (optional) is one of:\n");
fprintf(stdout," NL: LSB byte order, using new index format\n");
fprintf(stdout," NM: MSB byte order, using new index format\n");
fprintf(stdout," The following old format options are deprecated:\n");
fprintf(stdout," N: Native byte order\n");
fprintf(stdout," L: LSB (intel) byte order\n");
fprintf(stdout," M: MSB byte order\n");
fprintf(stdout," The default index_format on this system is: %s\n\n",
(byte_order == MS_NEW_LSB_ORDER) ? "NL" : "NM" );
exit(0);
}
if(argc >= 3)
depth = atoi(argv[2]);
if(argc >= 4) {
if( !strcasecmp(argv[3],"N" ))
byte_order = MS_NATIVE_ORDER;
if( !strcasecmp(argv[3],"L" ))
byte_order = MS_LSB_ORDER;
if( !strcasecmp(argv[3],"M" ))
byte_order = MS_MSB_ORDER;
if( !strcasecmp(argv[3],"NL" ))
byte_order = MS_NEW_LSB_ORDER;
if( !strcasecmp(argv[3],"NM" ))
byte_order = MS_NEW_MSB_ORDER;
}
if(msShapefileOpen(&shapefile, "rb", argv[1], MS_TRUE) == -1) {
fprintf(stdout, "Error opening shapefile %s.\n", argv[1]);
exit(0);
}
printf( "creating index of %s %s format\n",(byte_order < 1 ? "old (deprecated)" :"new"),
((byte_order == MS_NATIVE_ORDER) ? "native" :
((byte_order == MS_LSB_ORDER) || (byte_order == MS_NEW_LSB_ORDER)? " LSB":"MSB")));
tree = msCreateTree(&shapefile, depth);
if(!tree) {
#if MAX_SUBNODE == 2
fprintf(stdout, "Error generating binary tree.\n");
#else
fprintf(stdout, "Error generating quadtree.\n");
#endif
exit(0);
}
msWriteTree(tree, AddFileSuffix(argv[1], MS_INDEX_EXTENSION), byte_order);
msDestroyTree(tree);
/*
** Clean things up
*/
msShapefileClose(&shapefile);
return(0);
}