Revision 2639
Added by harris over 18 years ago
metacat_shapefile.cpp | ||
---|---|---|
1 |
|
|
2 | 1 |
/** |
3 | 2 |
* '$RCSfile$' |
4 | 3 |
* Copyright: 2000 Regents of the University of California and the |
... | ... | |
30 | 29 |
#include <stdlib.h> |
31 | 30 |
#include <string.h> |
32 | 31 |
#include <math.h> |
33 |
|
|
34 | 32 |
#include "iostream.h" |
35 |
|
|
36 | 33 |
#include <fstream> |
37 | 34 |
#include <string> |
38 | 35 |
#include <vector> |
... | ... | |
42 | 39 |
|
43 | 40 |
/************************************************************************ |
44 | 41 |
* Method to write the points array to the shape file and the associated |
45 |
* dbf file using the shapelib-1.2.xx libraries
|
|
42 |
* dbf file using the shapelib-1.2.ave_x libraries
|
|
46 | 43 |
* |
47 | 44 |
* @param nSHPType -- the type of shapefile (eg. poly vs point ) |
48 | 45 |
* |
49 | 46 |
* @param pszFilename -- the name of the shape file, sans the extension |
50 | 47 |
* |
51 |
* @param gridx -- this is the float value of the x-location (assuming points) |
|
48 |
* @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 |
|
52 | 50 |
* |
53 |
* @param gridy -- this is the float value of the y-location (assuming points) |
|
51 |
* @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 |
|
54 | 53 |
* |
55 |
* @param gridz -- this is the float value of the optional p-value
|
|
54 |
* @param pointz -- this is the float value of the optional p-value
|
|
56 | 55 |
* (assuming points) |
57 | 56 |
* |
58 | 57 |
* @param the number of lines in the coordinates arrays |
59 | 58 |
************************************************************************/ |
60 |
static void WritePointsArray ( int nSHPType, const char * pszFilename, |
|
61 |
float gridx[40000] [100], float gridy[40000] [100],float gridz[40000] [100], |
|
62 |
vector<string> urlvec, vector<string> docidvec, int number_data_lines ) { |
|
59 |
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 ) { |
|
63 | 69 |
|
64 | 70 |
SHPHandle hSHPHandle; |
65 | 71 |
SHPObject * psShape; |
... | ... | |
79 | 85 |
} |
80 | 86 |
|
81 | 87 |
//add to the dbf file attributes for ID, pval(elevation), url |
82 |
if ( DBFAddField( hDBF, "ID", FTDouble, 10, 10 ) == -1 )
|
|
88 |
if ( DBFAddField( hDBF, "ID", FTInteger, 10, 0 ) == -1 )
|
|
83 | 89 |
{ |
84 |
printf( "DBFAddField(%s,FTDouble,%d,0) failed.\n" );
|
|
90 |
printf( "DBFAddField(%s,FTInteger,%d,0) failed.\n" );
|
|
85 | 91 |
} |
86 |
if ( DBFAddField( hDBF, "pval", FTDouble, 10, 10 ) == -1 )
|
|
92 |
if ( DBFAddField( hDBF, "area_coef", FTDouble, 10, 4 ) == -1 )
|
|
87 | 93 |
{ |
88 | 94 |
printf( "DBFAddField(%s,FTDouble,%d,0) failed.\n" ); |
89 | 95 |
} |
... | ... | |
99 | 105 |
|
100 | 106 |
//cout<<" DBF Record Count --> "<<iRecord<<endl; |
101 | 107 |
int id_field = DBFGetFieldIndex( hDBF, "ID" ); |
102 |
int pval_field = DBFGetFieldIndex( hDBF, "pval" ); |
|
103 | 108 |
int url_field = DBFGetFieldIndex( hDBF, "url" ); |
104 | 109 |
int docid_field = DBFGetFieldIndex( hDBF, "docid" ); |
110 |
int area_field = DBFGetFieldIndex( hDBF, "area_coef" ); |
|
105 | 111 |
|
106 |
//cout << "id field -> " << id_field << endl; |
|
107 |
//cout << "pval field -> " << pval_field << endl; |
|
108 |
//cout << "url field -> " << url_field << endl; |
|
109 |
|
|
110 |
|
|
111 | 112 |
hSHPHandle = SHPCreate( pszFilename, nSHPType ); |
112 | 113 |
|
113 | 114 |
ii = 0; |
114 | 115 |
for ( ii = 0; ii < number_data_lines; ii++ ) |
115 | 116 |
{ |
116 |
double xx = ( gridx[2] [ii] ); |
|
117 |
double yy = ( gridy[2] [ii] ); |
|
118 |
double zz = ( gridz[2] [ii] ); |
|
117 |
double area_coef = sqrt( pow((max_pntx[2][ii]-min_pntx[2][ii]),2) + pow((max_pnty[2][ii]-min_pnty[2][ii]),2) ); |
|
118 |
double ave_x = ( (min_pntx[2][ii]+max_pntx[2][ii])/2 ); |
|
119 |
double ave_y = ( (min_pnty[2][ii]+max_pnty[2][ii])/2 ); |
|
120 |
double ave_z = ( pointz[2][ii] ); |
|
119 | 121 |
|
120 |
//if (xx < 180 && xx > -180 && yy < 90 && yy >-90 ) {
|
|
122 |
if (ave_x < 180 && ave_x > -180 && ave_y < 90 && ave_y >-90 ) {
|
|
121 | 123 |
|
122 |
psShape = SHPCreateObject( SHPT_POINT, ii, 0, NULL, NULL, 1, &xx, &yy, &zz, NULL );
|
|
124 |
psShape = SHPCreateObject( SHPT_POINT, ii, 0, NULL, NULL, 1, &ave_x, &ave_y, &ave_z, NULL );
|
|
123 | 125 |
|
124 | 126 |
SHPWriteObject( hSHPHandle, -1, psShape ); |
125 | 127 |
SHPDestroyObject( psShape ); |
126 | 128 |
//do the update to the dbf file too |
127 | 129 |
iRecord = DBFGetRecordCount( hDBF ); |
128 | 130 |
|
129 |
|
|
130 | 131 |
DBFWriteStringAttribute(hDBF, iRecord, url_field, urlvec[ii].c_str() ); |
131 | 132 |
DBFWriteStringAttribute(hDBF, iRecord, docid_field, docidvec[ii].c_str() ); |
132 |
DBFWriteDoubleAttribute(hDBF, iRecord, pval_field, 999 );
|
|
133 |
DBFWriteDoubleAttribute(hDBF, iRecord, id_field, ii );
|
|
134 |
// }
|
|
133 |
DBFWriteIntegerAttribute(hDBF, iRecord, id_field, int(ii) );
|
|
134 |
DBFWriteDoubleAttribute(hDBF, iRecord, area_field, area_coef );
|
|
135 |
} |
|
135 | 136 |
} |
136 | 137 |
//recompute the extents |
137 | 138 |
//SHPComputeExtents( psShape ); |
... | ... | |
163 | 164 |
std::cout<<" ## harris@nceas.ucsb.edu ##\n"; |
164 | 165 |
std::cout<<" ## Copyright 1999-2003 ##\n"; |
165 | 166 |
std::cout<<" ##><><><><><><><><><><><><><><><><><><><><><><><><>##\n"; |
166 |
|
|
167 |
|
|
167 | 168 |
std::cout << std::endl; |
168 | 169 |
|
169 | 170 |
} |
... | ... | |
180 | 181 |
} |
181 | 182 |
|
182 | 183 |
//these arrays are for the input x, y, z data |
183 |
float gridx[1000] [100]; |
|
184 |
float gridy[1000] [100]; |
|
185 |
float gridz[1000] [100]; |
|
184 |
float min_pntx[1000] [100]; |
|
185 |
float min_pnty[1000] [100]; |
|
186 |
float max_pntx[1000] [100]; |
|
187 |
float max_pnty[1000] [100]; |
|
188 |
|
|
189 |
float pointz[1000] [100]; |
|
186 | 190 |
vector<string> docidvec; |
187 | 191 |
vector<string> urlvec; |
188 | 192 |
|
... | ... | |
192 | 196 |
|
193 | 197 |
int cnt = 0; |
194 | 198 |
int number_lines = 0; |
195 |
char bufa[100]; |
|
196 |
char buf[100]; |
|
197 |
char buf1[100]; |
|
198 |
char buf2[100]; |
|
199 |
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]; |
|
199 | 205 |
|
200 | 206 |
SHPHandle hSHP; |
201 | 207 |
int nShapeType, nEntities, i, iPart; |
... | ... | |
208 | 214 |
exit( 2 ); |
209 | 215 |
} |
210 | 216 |
|
211 |
/* read the data into an array -- first the grid nodes */ |
|
217 |
/* read the data into an array -- first the grid nodes |
|
218 |
* |
|
219 |
* the data should have the following columns (from left to right) |
|
220 |
* |
|
221 |
* nrs.239.1 |
|
222 |
* -121.558333333333 |
|
223 |
* -121.558333333333 |
|
224 |
* 36.2083333333333 |
|
225 |
* 36.2083333333333 |
|
226 |
* http://metacat.nceas.ucsb.edu/knb/servlet/metacat?action=read&docid=nrs.239.1&qformat=knb |
|
227 |
*/ |
|
212 | 228 |
infile = fopen( argv[1], "r" ); |
213 | 229 |
char const * out_shape = argv[2]; |
214 | 230 |
while ( fgets( oneline, 199, infile ) ) { |
215 |
sscanf( oneline, "%s %s %s %s", bufa, buf, buf1, buf2 );
|
|
231 |
sscanf( oneline, "%s %s %s %s %s %s %s", buf_docid, buf_xmin, buf_xmax, buf_ymin, buf_ymax, buf_url);
|
|
216 | 232 |
|
217 | 233 |
/**** cout<<" docid -> " << bufa |
218 | 234 |
<<"\n cnt -> " << cnt |
... | ... | |
221 | 237 |
<<"\n p -> " << buf2 |
222 | 238 |
<<endl; *******/ |
223 | 239 |
|
224 |
gridx[2] [cnt] = atof( buf ); |
|
225 |
gridy[2] [cnt] = atof( buf1 ); |
|
226 |
gridz[2] [cnt] = atof( buf2 ); |
|
227 |
//char const * sLine = buf2; |
|
228 |
urlvec.push_back(buf2); |
|
229 |
docidvec.push_back(bufa); |
|
240 |
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 |
|
|
245 |
urlvec.push_back(buf_url); |
|
246 |
docidvec.push_back(buf_docid); |
|
247 |
|
|
230 | 248 |
cnt++; |
231 | 249 |
number_lines = cnt; |
232 | 250 |
} |
233 | 251 |
fclose( infile ); |
234 | 252 |
|
235 | 253 |
cout<<"creating a new shape file, with "<< number_lines <<" records "<<endl; |
236 |
WritePointsArray( SHPT_POINT, out_shape, gridx, gridy, gridz, |
|
237 |
urlvec, docidvec, number_lines ); |
|
254 |
WritePointsArray( SHPT_POINT, |
|
255 |
out_shape, |
|
256 |
min_pntx, |
|
257 |
max_pntx, |
|
258 |
min_pnty, |
|
259 |
max_pnty, |
|
260 |
pointz, |
|
261 |
urlvec, |
|
262 |
docidvec, |
|
263 |
number_lines ); |
|
238 | 264 |
|
239 | 265 |
#ifdef USE_DBMALLOC |
240 | 266 |
malloc_dump( 2 ); |
Also available in: Unified diff
Added an attribute to the metacat shapefile called area_coef which is a function
of the areal extent of the datapackage -- so a datapackage which was related to
a global collection would have a large extent, whereas a 3-foot squared plot
would have a small extent. Also fixed a bug related to the ID field that was
causing errors in ESRI apps.