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 |
3034
|
perry
|
#include "iostream"
|
33 |
2564
|
harris
|
#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 |
2915
|
harris
|
//
|
60 |
|
|
// 2/19/06 R Reeves modified array declarations for WritePointsArray funciton.
|
61 |
|
|
//
|
62 |
|
|
const int number_data_lines = 100;
|
63 |
|
|
static void WritePointsArray ( int nSHPType,
|
64 |
|
|
const char * pszFilename,
|
65 |
|
|
float min_pntx[][100],
|
66 |
|
|
float max_pntx[][100],
|
67 |
|
|
float min_pnty[][100],
|
68 |
|
|
float max_pnty[][100],
|
69 |
|
|
float pointz[][100],
|
70 |
|
|
vector<string> urlvec,
|
71 |
|
|
vector<string> docidvec,
|
72 |
|
|
int number_data_lines_x
|
73 |
|
|
)
|
74 |
|
|
{
|
75 |
|
|
SHPHandle hSHPHandle;
|
76 |
|
|
SHPObject * psShape;
|
77 |
|
|
double x, y, z, m, j, dx[number_data_lines], dy[number_data_lines], dz[number_data_lines];
|
78 |
|
|
int ii, iRecord, i;
|
79 |
|
|
char buf[20];
|
80 |
|
|
char intString;
|
81 |
|
|
//for the dbf file
|
82 |
|
|
DBFHandle hDBF;
|
83 |
2564
|
harris
|
|
84 |
2915
|
harris
|
//create the dbf file
|
85 |
|
|
hDBF = DBFCreate( pszFilename );
|
86 |
|
|
if ( hDBF == NULL )
|
87 |
|
|
{
|
88 |
|
|
printf( "DBFCreate(%s) failed.\n" );
|
89 |
|
|
exit( 2 );
|
90 |
|
|
}
|
91 |
2564
|
harris
|
|
92 |
2915
|
harris
|
//add to the dbf file attributes for ID, pval(elevation), url
|
93 |
|
|
if ( DBFAddField( hDBF, "ID", FTInteger, 10, 0 ) == -1 )
|
94 |
|
|
{
|
95 |
|
|
printf( "DBFAddField(%s,FTInteger,%d,0) failed.\n" );
|
96 |
|
|
}
|
97 |
|
|
if ( DBFAddField( hDBF, "area_coef", FTDouble, 10, 4 ) == -1 )
|
98 |
|
|
{
|
99 |
|
|
printf( "DBFAddField(%s,FTDouble,%d,0) failed.\n" );
|
100 |
|
|
}
|
101 |
|
|
if ( DBFAddField( hDBF, "url", FTString, 220, 0 ) == -1 )
|
102 |
|
|
{
|
103 |
|
|
printf( "DBFAddField(%s,FTString,%d,0) failed.\n" );
|
104 |
|
|
}
|
105 |
|
|
if ( DBFAddField( hDBF, "docid", FTString, 220, 0 ) == -1 )
|
106 |
|
|
{
|
107 |
|
|
printf( "DBFAddField(%s,FTString,%d,0) failed.\n" );
|
108 |
|
|
}
|
109 |
2564
|
harris
|
|
110 |
|
|
|
111 |
2915
|
harris
|
//cout<<" DBF Record Count --> "<<iRecord<<endl;
|
112 |
|
|
int id_field = DBFGetFieldIndex( hDBF, "ID" );
|
113 |
|
|
int url_field = DBFGetFieldIndex( hDBF, "url" );
|
114 |
|
|
int docid_field = DBFGetFieldIndex( hDBF, "docid" );
|
115 |
|
|
int area_field = DBFGetFieldIndex( hDBF, "area_coef" );
|
116 |
2564
|
harris
|
|
117 |
2915
|
harris
|
hSHPHandle = SHPCreate( pszFilename, nSHPType );
|
118 |
2564
|
harris
|
|
119 |
2915
|
harris
|
ii = 0;
|
120 |
3034
|
perry
|
for ( ii = 0; ii < number_data_lines_x; ii++ )
|
121 |
2915
|
harris
|
{
|
122 |
|
|
double area_coef = sqrt( pow((max_pntx[2][ii]-min_pntx[2][ii]),2) + pow((max_pnty[2][ii]-min_pnty[2][ii]),2) );
|
123 |
|
|
double ave_x = ( (min_pntx[2][ii]+max_pntx[2][ii])/2 );
|
124 |
|
|
double ave_y = ( (min_pnty[2][ii]+max_pnty[2][ii])/2 );
|
125 |
|
|
double ave_z = ( pointz[2][ii] );
|
126 |
2564
|
harris
|
|
127 |
3034
|
perry
|
cout << "area coef: " << area_coef << endl;
|
128 |
|
|
|
129 |
2915
|
harris
|
if (ave_x < 180 && ave_x > -180 && ave_y < 90 && ave_y >-90 ) {
|
130 |
2564
|
harris
|
|
131 |
2915
|
harris
|
psShape = SHPCreateObject( SHPT_POINT, ii, 0, NULL, NULL, 1, &ave_x, &ave_y, &ave_z, NULL );
|
132 |
2564
|
harris
|
|
133 |
2915
|
harris
|
SHPWriteObject( hSHPHandle, -1, psShape );
|
134 |
|
|
SHPDestroyObject( psShape );
|
135 |
|
|
//do the update to the dbf file too
|
136 |
|
|
iRecord = DBFGetRecordCount( hDBF );
|
137 |
3034
|
perry
|
|
138 |
2915
|
harris
|
DBFWriteStringAttribute(hDBF, iRecord, url_field, urlvec[ii].c_str() );
|
139 |
|
|
DBFWriteStringAttribute(hDBF, iRecord, docid_field, docidvec[ii].c_str() );
|
140 |
|
|
DBFWriteIntegerAttribute(hDBF, iRecord, id_field, int(ii) );
|
141 |
|
|
DBFWriteDoubleAttribute(hDBF, iRecord, area_field, area_coef );
|
142 |
|
|
}
|
143 |
|
|
}
|
144 |
|
|
//recompute the extents
|
145 |
|
|
//SHPComputeExtents( psShape );
|
146 |
|
|
//cout << "xmin: " << psShape->dfXMin << " xmax: " << psShape->dfXMax << endl;
|
147 |
|
|
//cout << "ymin: " << psShape->dfYMin << " ymax: " << psShape->dfYMax << endl;
|
148 |
|
|
DBFClose( hDBF );
|
149 |
|
|
SHPClose( hSHPHandle );
|
150 |
2564
|
harris
|
}
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
/** getUsage -- function that prints to stdout the usage for this driver */
|
154 |
2598
|
harris
|
static void getUsage()
|
155 |
2564
|
harris
|
{
|
156 |
2915
|
harris
|
cout<<"usage: metacat2shape metacat_export.txt test.shp\n\n";
|
157 |
|
|
cout<<" where:\n\n";
|
158 |
|
|
cout<<" input -- is an input ASCII file, that contains the following cols:\n";
|
159 |
|
|
cout<<" -> docid -- the document id\n";
|
160 |
|
|
cout<<" -> x -- the xlocation\n";
|
161 |
|
|
cout<<" -> y -- the y location\n";
|
162 |
|
|
cout<<" -> url -- the url to the document\n";
|
163 |
2564
|
harris
|
|
164 |
2915
|
harris
|
cout<<" shapefile -- {output} is a ESRI shapefile\n\n";
|
165 |
2564
|
harris
|
|
166 |
2915
|
harris
|
cout<<" see:\n";
|
167 |
|
|
cout<<" example: metacat2shape metacat_export.txt test.shp\n\n";
|
168 |
2564
|
harris
|
|
169 |
2915
|
harris
|
cout<<" ##><><><><><><><><><><><><><><><><><><><><><><><><>##\n";
|
170 |
|
|
cout<<" ## John Harris ##\n";
|
171 |
|
|
cout<<" ## harris@nceas.ucsb.edu ##\n";
|
172 |
|
|
cout<<" ## Copyright 1999-2003 ##\n";
|
173 |
|
|
cout<<" ##><><><><><><><><><><><><><><><><><><><><><><><><>##\n";
|
174 |
2639
|
harris
|
|
175 |
2915
|
harris
|
cout << endl;
|
176 |
2564
|
harris
|
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
|
181 |
2915
|
harris
|
int main( int argc, char * * argv )
|
182 |
2564
|
harris
|
{
|
183 |
|
|
//get the usage info if the inputs are not right
|
184 |
2915
|
harris
|
if ( argc != 3 )
|
185 |
|
|
{
|
186 |
2564
|
harris
|
getUsage();
|
187 |
2915
|
harris
|
exit( 1 );
|
188 |
2564
|
harris
|
}
|
189 |
2915
|
harris
|
|
190 |
2564
|
harris
|
//these arrays are for the input x, y, z data
|
191 |
2915
|
harris
|
float min_pntx[1000] [100];
|
192 |
|
|
float min_pnty[1000] [100];
|
193 |
|
|
float max_pntx[1000] [100];
|
194 |
|
|
float max_pnty[1000] [100];
|
195 |
2639
|
harris
|
|
196 |
2915
|
harris
|
float pointz[1000] [100];
|
197 |
|
|
vector<string> docidvec;
|
198 |
|
|
vector<string> urlvec;
|
199 |
2564
|
harris
|
|
200 |
2915
|
harris
|
FILE * infile;
|
201 |
2564
|
harris
|
char oneline[200];
|
202 |
2915
|
harris
|
char * value;
|
203 |
2564
|
harris
|
|
204 |
|
|
int cnt = 0;
|
205 |
|
|
int number_lines = 0;
|
206 |
2639
|
harris
|
char buf_docid[100];
|
207 |
|
|
char buf_xmin[100];
|
208 |
|
|
char buf_xmax[100];
|
209 |
|
|
char buf_ymin[100];
|
210 |
|
|
char buf_ymax[100];
|
211 |
|
|
char buf_url[100];
|
212 |
2564
|
harris
|
|
213 |
|
|
SHPHandle hSHP;
|
214 |
|
|
int nShapeType, nEntities, i, iPart;
|
215 |
2915
|
harris
|
const char * pszPlus;
|
216 |
2564
|
harris
|
|
217 |
|
|
//test the reading of the file
|
218 |
2915
|
harris
|
if ( ( infile = fopen( argv[1], "r" ) ) == ( FILE * ) NULL )
|
219 |
|
|
{
|
220 |
|
|
fprintf( stderr, "Can't read the file: %s.\n", argv[1] );
|
221 |
|
|
exit( 2 );
|
222 |
2564
|
harris
|
}
|
223 |
|
|
|
224 |
2639
|
harris
|
/* read the data into an array -- first the grid nodes
|
225 |
|
|
*
|
226 |
|
|
* the data should have the following columns (from left to right)
|
227 |
|
|
*
|
228 |
|
|
* nrs.239.1
|
229 |
|
|
* -121.558333333333
|
230 |
|
|
* -121.558333333333
|
231 |
|
|
* 36.2083333333333
|
232 |
|
|
* 36.2083333333333
|
233 |
|
|
* http://metacat.nceas.ucsb.edu/knb/servlet/metacat?action=read&docid=nrs.239.1&qformat=knb
|
234 |
|
|
*/
|
235 |
2915
|
harris
|
infile = fopen( argv[1], "r" );
|
236 |
|
|
char const * out_shape = argv[2];
|
237 |
|
|
while ( fgets( oneline, 199, infile ) ) {
|
238 |
|
|
sscanf( oneline, "%s %s %s %s %s %s %s", buf_docid, buf_xmin, buf_xmax, buf_ymin, buf_ymax, buf_url);
|
239 |
2564
|
harris
|
|
240 |
|
|
/**** cout<<" docid -> " << bufa
|
241 |
|
|
<<"\n cnt -> " << cnt
|
242 |
|
|
<<"\n x -> " << buf
|
243 |
|
|
<<"\n y -> " << buf1
|
244 |
|
|
<<"\n p -> " << buf2
|
245 |
|
|
<<endl; *******/
|
246 |
|
|
|
247 |
2915
|
harris
|
min_pntx[2] [cnt] = atof(buf_xmin);
|
248 |
|
|
max_pntx[2] [cnt] = atof(buf_xmax);
|
249 |
|
|
min_pnty[2] [cnt] = atof(buf_ymin);
|
250 |
|
|
max_pnty[2] [cnt] = atof(buf_ymax);
|
251 |
2639
|
harris
|
|
252 |
|
|
urlvec.push_back(buf_url);
|
253 |
|
|
docidvec.push_back(buf_docid);
|
254 |
|
|
|
255 |
2564
|
harris
|
cnt++;
|
256 |
|
|
number_lines = cnt;
|
257 |
|
|
}
|
258 |
2915
|
harris
|
fclose( infile );
|
259 |
2564
|
harris
|
|
260 |
2915
|
harris
|
cout<<"creating a new shape file, with "<< number_lines <<" records "<<endl;
|
261 |
|
|
WritePointsArray( SHPT_POINT,
|
262 |
|
|
out_shape,
|
263 |
|
|
min_pntx,
|
264 |
|
|
max_pntx,
|
265 |
|
|
min_pnty,
|
266 |
|
|
max_pnty,
|
267 |
|
|
pointz,
|
268 |
|
|
urlvec,
|
269 |
|
|
docidvec,
|
270 |
|
|
number_lines );
|
271 |
2564
|
harris
|
|
272 |
|
|
#ifdef USE_DBMALLOC
|
273 |
2915
|
harris
|
malloc_dump( 2 );
|
274 |
2564
|
harris
|
#endif
|
275 |
2915
|
harris
|
exit( 0 );
|
276 |
2564
|
harris
|
}
|