1 |
2565
|
harris
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Copyright: 2000 Regents of the University of California and the
|
4 |
|
|
* National Center for Ecological Analysis and Synthesis
|
5 |
|
|
* Authors: John Harris
|
6 |
|
|
* Release: @release@
|
7 |
|
|
*
|
8 |
|
|
* '$Author$'
|
9 |
|
|
* '$Date$'
|
10 |
|
|
* '$Revision$'
|
11 |
|
|
*
|
12 |
|
|
* This program is free software; you can redistribute it and/or modify
|
13 |
|
|
* it under the terms of the GNU General Public License as published by
|
14 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
15 |
|
|
* (at your option) any later version.
|
16 |
|
|
*
|
17 |
|
|
* This program is distributed in the hope that it will be useful,
|
18 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
|
|
* GNU General Public License for more details.
|
21 |
|
|
*
|
22 |
|
|
* You should have received a copy of the GNU General Public License
|
23 |
|
|
* along with this program; if not, write to the Free Software
|
24 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25 |
|
|
*/
|
26 |
2564
|
harris
|
|
27 |
|
|
#include "shapefil.h"
|
28 |
|
|
#include <stdio.h>
|
29 |
|
|
#include <stdlib.h>
|
30 |
|
|
#include <string.h>
|
31 |
|
|
#include <math.h>
|
32 |
|
|
#include "iostream.h"
|
33 |
|
|
#include <fstream>
|
34 |
|
|
#include <string>
|
35 |
|
|
#include <vector>
|
36 |
|
|
|
37 |
|
|
using namespace std;
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
/************************************************************************
|
41 |
|
|
* Method to write the points array to the shape file and the associated
|
42 |
2639
|
harris
|
* dbf file using the shapelib-1.2.ave_x libraries
|
43 |
2564
|
harris
|
*
|
44 |
|
|
* @param nSHPType -- the type of shapefile (eg. poly vs point )
|
45 |
|
|
*
|
46 |
|
|
* @param pszFilename -- the name of the shape file, sans the extension
|
47 |
|
|
*
|
48 |
2639
|
harris
|
* @param min_pntx -- this is the float value of the min x-location
|
49 |
|
|
* @param max_pntx -- this is the float value of the max x-location
|
50 |
2564
|
harris
|
*
|
51 |
2639
|
harris
|
* @param min_pnty -- this is the float value of the min y-location
|
52 |
|
|
* @param max_pnty -- this is the float value of the max y-location
|
53 |
2564
|
harris
|
*
|
54 |
2639
|
harris
|
* @param pointz -- this is the float value of the optional p-value
|
55 |
2564
|
harris
|
* (assuming points)
|
56 |
|
|
*
|
57 |
|
|
* @param the number of lines in the coordinates arrays
|
58 |
|
|
************************************************************************/
|
59 |
2907
|
harris
|
static void WritePointsArray(int nSHPType,
|
60 |
|
|
const char *pszFilename,
|
61 |
|
|
float min_pntx[40000][100],
|
62 |
|
|
float max_pntx[40000][100],
|
63 |
|
|
float min_pnty[40000][100],
|
64 |
|
|
float max_pnty[40000][100],
|
65 |
|
|
float pointz[40000][100],
|
66 |
|
|
vector < string > urlvec,
|
67 |
|
|
vector < string > docidvec,
|
68 |
|
|
int number_data_lines)
|
69 |
|
|
{
|
70 |
2564
|
harris
|
|
71 |
2907
|
harris
|
SHPHandle hSHPHandle;
|
72 |
|
|
SHPObject *psShape;
|
73 |
|
|
double x, y, z, m, j, dx[number_data_lines], dy[number_data_lines],
|
74 |
|
|
dz[number_data_lines];
|
75 |
|
|
int ii, iRecord, i;
|
76 |
|
|
char buf[20];
|
77 |
|
|
char intString;
|
78 |
|
|
//for the dbf file
|
79 |
|
|
DBFHandle hDBF;
|
80 |
2564
|
harris
|
|
81 |
2907
|
harris
|
//create the dbf file
|
82 |
|
|
hDBF = DBFCreate(pszFilename);
|
83 |
|
|
if (hDBF == NULL) {
|
84 |
|
|
printf("DBFCreate(%s) failed.\n");
|
85 |
|
|
exit(2);
|
86 |
|
|
}
|
87 |
|
|
//add to the dbf file attributes for ID, pval(elevation), url
|
88 |
|
|
if (DBFAddField(hDBF, "ID", FTInteger, 10, 0) == -1) {
|
89 |
|
|
printf("DBFAddField(%s,FTInteger,%d,0) failed.\n");
|
90 |
|
|
}
|
91 |
|
|
if (DBFAddField(hDBF, "area_coef", FTDouble, 10, 4) == -1) {
|
92 |
|
|
printf("DBFAddField(%s,FTDouble,%d,0) failed.\n");
|
93 |
|
|
}
|
94 |
|
|
if (DBFAddField(hDBF, "url", FTString, 220, 0) == -1) {
|
95 |
|
|
printf("DBFAddField(%s,FTString,%d,0) failed.\n");
|
96 |
|
|
}
|
97 |
|
|
if (DBFAddField(hDBF, "docid", FTString, 220, 0) == -1) {
|
98 |
|
|
printf("DBFAddField(%s,FTString,%d,0) failed.\n");
|
99 |
|
|
}
|
100 |
2564
|
harris
|
|
101 |
2907
|
harris
|
//cout<<" DBF Record Count --> "<<iRecord<<endl;
|
102 |
|
|
int id_field = DBFGetFieldIndex(hDBF, "ID");
|
103 |
|
|
int url_field = DBFGetFieldIndex(hDBF, "url");
|
104 |
|
|
int docid_field = DBFGetFieldIndex(hDBF, "docid");
|
105 |
|
|
int area_field = DBFGetFieldIndex(hDBF, "area_coef");
|
106 |
2564
|
harris
|
|
107 |
2907
|
harris
|
hSHPHandle = SHPCreate(pszFilename, nSHPType);
|
108 |
2564
|
harris
|
|
109 |
2907
|
harris
|
ii = 0;
|
110 |
|
|
for (ii = 0; ii < number_data_lines; ii++) {
|
111 |
|
|
double area_coef =
|
112 |
|
|
sqrt(pow((max_pntx[2][ii] - min_pntx[2][ii]), 2) +
|
113 |
|
|
pow((max_pnty[2][ii] - min_pnty[2][ii]), 2));
|
114 |
|
|
double ave_x = ((min_pntx[2][ii] + max_pntx[2][ii]) / 2);
|
115 |
|
|
double ave_y = ((min_pnty[2][ii] + max_pnty[2][ii]) / 2);
|
116 |
|
|
double ave_z = (pointz[2][ii]);
|
117 |
2564
|
harris
|
|
118 |
2907
|
harris
|
if (ave_x < 180 && ave_x > -180 && ave_y < 90 && ave_y > -90) {
|
119 |
2564
|
harris
|
|
120 |
2907
|
harris
|
psShape =
|
121 |
|
|
SHPCreateObject(SHPT_POINT, ii, 0, NULL, NULL, 1, &ave_x, &ave_y,
|
122 |
|
|
&ave_z, NULL);
|
123 |
2564
|
harris
|
|
124 |
2907
|
harris
|
SHPWriteObject(hSHPHandle, -1, psShape);
|
125 |
|
|
SHPDestroyObject(psShape);
|
126 |
|
|
//do the update to the dbf file too
|
127 |
|
|
iRecord = DBFGetRecordCount(hDBF);
|
128 |
2564
|
harris
|
|
129 |
2907
|
harris
|
DBFWriteStringAttribute(hDBF, iRecord, url_field,
|
130 |
|
|
urlvec[ii].c_str());
|
131 |
|
|
DBFWriteStringAttribute(hDBF, iRecord, docid_field,
|
132 |
|
|
docidvec[ii].c_str());
|
133 |
|
|
DBFWriteIntegerAttribute(hDBF, iRecord, id_field, int (ii));
|
134 |
|
|
DBFWriteDoubleAttribute(hDBF, iRecord, area_field, area_coef);
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
//recompute the extents
|
138 |
|
|
//SHPComputeExtents( psShape );
|
139 |
|
|
//cout << "xmin: " << psShape->dfXMin << " xmax: " << psShape->dfXMax << endl;
|
140 |
|
|
//cout << "ymin: " << psShape->dfYMin << " ymax: " << psShape->dfYMax << endl;
|
141 |
|
|
DBFClose(hDBF);
|
142 |
|
|
SHPClose(hSHPHandle);
|
143 |
2564
|
harris
|
}
|
144 |
|
|
|
145 |
|
|
|
146 |
|
|
/** getUsage -- function that prints to stdout the usage for this driver */
|
147 |
2598
|
harris
|
static void getUsage()
|
148 |
2564
|
harris
|
{
|
149 |
2907
|
harris
|
std::cout << "usage: metacat2shape metacat_export.txt test.shp\n\n";
|
150 |
|
|
std::cout << " where:\n\n";
|
151 |
|
|
std::
|
152 |
|
|
cout <<
|
153 |
|
|
" input -- is an input ASCII file, that contains the following cols:\n";
|
154 |
|
|
std::cout << " -> docid -- the document id\n";
|
155 |
|
|
std::cout << " -> x -- the xlocation\n";
|
156 |
|
|
std::cout << " -> y -- the y location\n";
|
157 |
|
|
std::cout << " -> url -- the url to the document\n";
|
158 |
2564
|
harris
|
|
159 |
2907
|
harris
|
std::cout << " shapefile -- {output} is a ESRI shapefile\n\n";
|
160 |
2564
|
harris
|
|
161 |
2907
|
harris
|
std::cout << " see:\n";
|
162 |
|
|
std::cout << " example: metacat2shape metacat_export.txt test.shp\n\n";
|
163 |
2564
|
harris
|
|
164 |
2907
|
harris
|
std::cout << " ##><><><><><><><><><><><><><><><><><><><><><><><><>##\n";
|
165 |
|
|
std::cout << " ## John Harris ##\n";
|
166 |
|
|
std::cout << " ## harris@nceas.ucsb.edu ##\n";
|
167 |
|
|
std::cout << " ## Copyright 1999-2003 ##\n";
|
168 |
|
|
std::cout << " ##><><><><><><><><><><><><><><><><><><><><><><><><>##\n";
|
169 |
2639
|
harris
|
|
170 |
2598
|
harris
|
std::cout << std::endl;
|
171 |
2564
|
harris
|
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
2907
|
harris
|
int main(int argc, char **argv)
|
177 |
2564
|
harris
|
{
|
178 |
|
|
//get the usage info if the inputs are not right
|
179 |
2907
|
harris
|
if (argc != 3) {
|
180 |
2564
|
harris
|
getUsage();
|
181 |
2907
|
harris
|
exit(1);
|
182 |
2564
|
harris
|
}
|
183 |
|
|
//these arrays are for the input x, y, z data
|
184 |
2907
|
harris
|
float min_pntx[1000][100];
|
185 |
|
|
float min_pnty[1000][100];
|
186 |
|
|
float max_pntx[1000][100];
|
187 |
|
|
float max_pnty[1000][100];
|
188 |
2639
|
harris
|
|
189 |
2907
|
harris
|
float pointz[1000][100];
|
190 |
|
|
vector < string > docidvec;
|
191 |
|
|
vector < string > urlvec;
|
192 |
2564
|
harris
|
|
193 |
2907
|
harris
|
FILE *infile;
|
194 |
2564
|
harris
|
char oneline[200];
|
195 |
2907
|
harris
|
char *value;
|
196 |
2564
|
harris
|
|
197 |
|
|
int cnt = 0;
|
198 |
|
|
int number_lines = 0;
|
199 |
2639
|
harris
|
char buf_docid[100];
|
200 |
|
|
char buf_xmin[100];
|
201 |
|
|
char buf_xmax[100];
|
202 |
|
|
char buf_ymin[100];
|
203 |
|
|
char buf_ymax[100];
|
204 |
|
|
char buf_url[100];
|
205 |
2564
|
harris
|
|
206 |
|
|
SHPHandle hSHP;
|
207 |
|
|
int nShapeType, nEntities, i, iPart;
|
208 |
2907
|
harris
|
const char *pszPlus;
|
209 |
2564
|
harris
|
|
210 |
|
|
//test the reading of the file
|
211 |
2907
|
harris
|
if ((infile = fopen(argv[1], "r")) == (FILE *) NULL) {
|
212 |
|
|
fprintf(stderr, "Can't read the file: %s.\n", argv[1]);
|
213 |
|
|
exit(2);
|
214 |
2564
|
harris
|
}
|
215 |
|
|
|
216 |
2639
|
harris
|
/* read the data into an array -- first the grid nodes
|
217 |
|
|
*
|
218 |
|
|
* the data should have the following columns (from left to right)
|
219 |
|
|
*
|
220 |
|
|
* nrs.239.1
|
221 |
|
|
* -121.558333333333
|
222 |
|
|
* -121.558333333333
|
223 |
|
|
* 36.2083333333333
|
224 |
|
|
* 36.2083333333333
|
225 |
|
|
* http://metacat.nceas.ucsb.edu/knb/servlet/metacat?action=read&docid=nrs.239.1&qformat=knb
|
226 |
|
|
*/
|
227 |
2907
|
harris
|
infile = fopen(argv[1], "r");
|
228 |
|
|
char const *out_shape = argv[2];
|
229 |
|
|
while (fgets(oneline, 199, infile)) {
|
230 |
|
|
sscanf(oneline, "%s %s %s %s %s %s %s", buf_docid, buf_xmin, buf_xmax,
|
231 |
|
|
buf_ymin, buf_ymax, buf_url);
|
232 |
2564
|
harris
|
|
233 |
|
|
/**** cout<<" docid -> " << bufa
|
234 |
|
|
<<"\n cnt -> " << cnt
|
235 |
|
|
<<"\n x -> " << buf
|
236 |
|
|
<<"\n y -> " << buf1
|
237 |
|
|
<<"\n p -> " << buf2
|
238 |
|
|
<<endl; *******/
|
239 |
|
|
|
240 |
2907
|
harris
|
min_pntx[2][cnt] = atof(buf_xmin);
|
241 |
|
|
max_pntx[2][cnt] = atof(buf_xmax);
|
242 |
|
|
min_pnty[2][cnt] = atof(buf_ymin);
|
243 |
|
|
max_pnty[2][cnt] = atof(buf_ymax);
|
244 |
2639
|
harris
|
|
245 |
|
|
urlvec.push_back(buf_url);
|
246 |
|
|
docidvec.push_back(buf_docid);
|
247 |
|
|
|
248 |
2564
|
harris
|
cnt++;
|
249 |
|
|
number_lines = cnt;
|
250 |
|
|
}
|
251 |
2907
|
harris
|
fclose(infile);
|
252 |
2564
|
harris
|
|
253 |
2907
|
harris
|
cout << "creating a new shape file, with " << number_lines << " records "
|
254 |
|
|
<< endl;
|
255 |
|
|
WritePointsArray(SHPT_POINT, out_shape, min_pntx, max_pntx, min_pnty,
|
256 |
|
|
max_pnty, pointz, urlvec, docidvec, number_lines);
|
257 |
2564
|
harris
|
|
258 |
|
|
#ifdef USE_DBMALLOC
|
259 |
2907
|
harris
|
malloc_dump(2);
|
260 |
2564
|
harris
|
#endif
|
261 |
2907
|
harris
|
exit(0);
|
262 |
2564
|
harris
|
}
|