Project

General

Profile

1

    
2
/**
3
 *  '$RCSfile$'
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: John Harris
7
 *    Release: @release@
8
 *
9
 *   '$Author: harris $'
10
 *     '$Date: 2005-09-23 11:19:38 -0700 (Fri, 23 Sep 2005) $'
11
 * '$Revision: 2598 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

    
28
#include "shapefil.h"
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <string.h>
32
#include <math.h>
33

    
34
#include "iostream.h"
35

    
36
#include <fstream>
37
#include <string>
38
#include <vector>
39

    
40
using namespace std;
41

    
42

    
43
/************************************************************************
44
 * Method to write the points array to the shape file and the associated
45
 * dbf file using the shapelib-1.2.xx libraries
46
 *
47
 * @param nSHPType -- the type of shapefile (eg. poly vs point )
48
 *
49
 * @param pszFilename -- the name of the shape file, sans the extension
50
 *
51
 * @param gridx -- this is the float value of the x-location (assuming points)
52
 *
53
 * @param gridy -- this is the float value of the y-location (assuming points)
54
 *
55
 * @param gridz -- this is the float value of the optional p-value
56
 * (assuming points)
57
 *
58
 * @param the number of lines in the coordinates arrays
59
 ************************************************************************/
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 ) {
63

    
64
       SHPHandle hSHPHandle;
65
       SHPObject * psShape;
66
       double x, y, z, m, j, dx[number_data_lines], dy[number_data_lines], dz[number_data_lines];
67
       int ii, iRecord, i;
68
       char buf[20];
69
       char intString;
70
       //for the dbf file
71
       DBFHandle hDBF;
72

    
73
       //create the dbf file
74
       hDBF = DBFCreate( pszFilename );
75
       if ( hDBF == NULL )
76
       {
77
         printf( "DBFCreate(%s) failed.\n" );
78
         exit( 2 );
79
       }
80

    
81
       //add to the dbf file attributes for ID, pval(elevation), url
82
       if ( DBFAddField( hDBF, "ID", FTDouble, 10, 10 ) == -1 )
83
       {
84
         printf( "DBFAddField(%s,FTDouble,%d,0) failed.\n" );
85
       }
86
       if ( DBFAddField( hDBF, "pval", FTDouble, 10, 10 ) == -1 )
87
       {
88
         printf( "DBFAddField(%s,FTDouble,%d,0) failed.\n" );
89
       }
90
       if ( DBFAddField( hDBF, "url", FTString, 220, 0 ) == -1 )
91
       {
92
         printf( "DBFAddField(%s,FTString,%d,0) failed.\n" );
93
       }
94
       if ( DBFAddField( hDBF, "docid", FTString, 220, 0 ) == -1 )
95
       {
96
         printf( "DBFAddField(%s,FTString,%d,0) failed.\n" );
97
       }
98

    
99

    
100
      //cout<<" DBF Record Count --> "<<iRecord<<endl;
101
      int id_field = DBFGetFieldIndex( hDBF, "ID" );
102
      int pval_field = DBFGetFieldIndex( hDBF, "pval" );
103
      int url_field = DBFGetFieldIndex( hDBF, "url" );
104
      int docid_field = DBFGetFieldIndex( hDBF, "docid" );
105

    
106
      //cout << "id field -> " << id_field << endl;
107
      //cout << "pval field -> " << pval_field << endl;
108
      //cout << "url field -> " << url_field << endl;
109

    
110

    
111
      hSHPHandle = SHPCreate( pszFilename, nSHPType );
112

    
113
       ii = 0;
114
       for ( ii = 0; ii < number_data_lines; ii++ )
115
       {
116
         double xx = ( gridx[2] [ii] );
117
         double yy = ( gridy[2] [ii] );
118
         double zz = ( gridz[2] [ii] );
119

    
120
         //if (xx < 180 && xx > -180 && yy < 90 && yy >-90 ) {
121

    
122
         psShape = SHPCreateObject( SHPT_POINT, ii, 0, NULL, NULL, 1, &xx, &yy, &zz, NULL );
123

    
124
         SHPWriteObject( hSHPHandle, -1, psShape );
125
         SHPDestroyObject( psShape );
126
         //do the update to the dbf file too
127
         iRecord = DBFGetRecordCount( hDBF );
128

    
129

    
130
        DBFWriteStringAttribute(hDBF, iRecord, url_field, urlvec[ii].c_str() );
131
        DBFWriteStringAttribute(hDBF, iRecord, docid_field, docidvec[ii].c_str() );
132
        DBFWriteDoubleAttribute(hDBF, iRecord, pval_field, 999 );
133
        DBFWriteDoubleAttribute(hDBF, iRecord, id_field, ii );
134
       //  }
135
       }
136
       //recompute the extents
137
       //SHPComputeExtents( psShape );
138
       //cout << "xmin: " << psShape->dfXMin << " xmax: " << psShape->dfXMax << endl;
139
       //cout << "ymin: " << psShape->dfYMin << " ymax: " << psShape->dfYMax << endl;
140
       DBFClose( hDBF );
141
       SHPClose( hSHPHandle );
142
}
143

    
144

    
145
/** getUsage -- function that prints to stdout the usage for this driver */
146
static void getUsage()
147
{
148
  std::cout<<"usage: metacat2shape metacat_export.txt test.shp\n\n";
149
  std::cout<<"  where:\n\n";
150
  std::cout<<"    input -- is an input ASCII file, that contains the following cols:\n";
151
  std::cout<<"          -> docid -- the document id\n";
152
  std::cout<<"          -> x -- the xlocation\n";
153
  std::cout<<"          -> y -- the y location\n";
154
  std::cout<<"          -> url -- the url to the document\n";
155

    
156
  std::cout<<"          shapefile -- {output} is a ESRI shapefile\n\n";
157

    
158
  std::cout<<"  see:\n";
159
  std::cout<<"  example: metacat2shape metacat_export.txt test.shp\n\n";
160

    
161
  std::cout<<"  ##><><><><><><><><><><><><><><><><><><><><><><><><>##\n";
162
  std::cout<<"  ##           John Harris                           ##\n";
163
  std::cout<<"  ##           harris@nceas.ucsb.edu                 ##\n";
164
  std::cout<<"  ##           Copyright 1999-2003                   ##\n";
165
  std::cout<<"  ##><><><><><><><><><><><><><><><><><><><><><><><><>##\n";
166
  
167
  std::cout << std::endl;
168

    
169
}
170

    
171

    
172

    
173
int main( int argc, char * * argv )
174
{
175
  //get the usage info if the inputs are not right
176
  if ( argc != 3 )
177
  {
178
    getUsage();
179
    exit( 1 );
180
  }
181

    
182
  //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];
186
  vector<string> docidvec;
187
  vector<string> urlvec;
188

    
189
  FILE * infile;
190
  char oneline[200];
191
  char * value;
192

    
193
  int cnt = 0;
194
  int number_lines = 0;
195
  char bufa[100];
196
  char buf[100];
197
  char buf1[100];
198
  char buf2[100];
199

    
200
  SHPHandle hSHP;
201
  int nShapeType, nEntities, i, iPart;
202
  const char * pszPlus;
203

    
204
  //test the reading of the file
205
  if ( ( infile = fopen( argv[1], "r" ) ) == ( FILE * ) NULL )
206
  {
207
    fprintf( stderr, "Can't read the file: %s.\n", argv[1] );
208
    exit( 2 );
209
  }
210

    
211
  /* read the data into an array -- first the grid nodes */
212
  infile = fopen( argv[1], "r" );
213
  char const * out_shape = argv[2];
214
  while ( fgets( oneline, 199, infile ) ) {
215
    sscanf( oneline, "%s %s %s %s", bufa, buf, buf1, buf2 );
216

    
217
    /**** cout<<" docid -> " << bufa
218
      <<"\n cnt -> " << cnt
219
      <<"\n x -> " << buf
220
      <<"\n y -> " << buf1
221
      <<"\n p -> " << buf2
222
      <<endl; *******/
223

    
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);
230
    cnt++;
231
    number_lines = cnt;
232
  }
233
  fclose( infile );
234

    
235
  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 );
238

    
239
#ifdef USE_DBMALLOC
240
  malloc_dump( 2 );
241
#endif
242
  exit( 0 );
243
}
(1-1/2)