Project

General

Profile

« Previous | Next » 

Revision 2564

Added by harris about 19 years ago

This is the c++ code that is used to write the shapefile. This code requires
the shapelib.

View differences:

src/spatial/cpp/metacat_shapefile.cpp
1
/** ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
2
****************************************************************************
3
 Copyright (C) 2001 Numeric Solutions.  All Rights Reserved.
4

  
5
  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
6
 License as published by the Free Software Foundation; either
7
 version 2.1 of the License, or (at your option) any later version.
8

  
9
  This library is distributed in the hope that it will be useful,
10
 but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
12

  
13
Further, this software is distributed without any warranty that it is
14
 free of the rightful claim of any third person regarding infringement
15
 or the like.  Any license provided herein, whether implied or
16
 otherwise, applies only to this software file.  Patent licenses, if
17
 any, provided herein do not apply to combinations of this program with other software, or any other product whatsoever.
18

  
19
You should have received a copy of the GNU Lesser General Public
20
 License along with this library; if not, write to the Free Software
21
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22

  
23
  Contact information: Numeric Solutions support@numericsolutions.com, or:
24

  
25
http://www.numericsolutions.com
26

  
27
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff */
28

  
29

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

  
36
#include "iostream.h"
37

  
38
#include <fstream>
39
#include <string>
40
#include <vector>
41

  
42
using namespace std;
43

  
44

  
45
/************************************************************************
46
 * Method to write the points array to the shape file and the associated
47
 * dbf file using the shapelib-1.2.xx libraries
48
 *
49
 * @param nSHPType -- the type of shapefile (eg. poly vs point )
50
 *
51
 * @param pszFilename -- the name of the shape file, sans the extension
52
 *
53
 * @param gridx -- this is the float value of the x-location (assuming points)
54
 *
55
 * @param gridy -- this is the float value of the y-location (assuming points)
56
 *
57
 * @param gridz -- this is the float value of the optional p-value
58
 * (assuming points)
59
 *
60
 * @param the number of lines in the coordinates arrays
61
 ************************************************************************/
62
static void WritePointsArray ( int nSHPType, const char * pszFilename,
63
  float gridx[40000] [100], float gridy[40000] [100],float gridz[40000] [100],
64
  vector<string> urlvec, vector<string> docidvec, int number_data_lines ) {
65

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

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

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

  
101

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

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

  
112

  
113
      hSHPHandle = SHPCreate( pszFilename, nSHPType );
114

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

  
122
         //if (xx < 180 && xx > -180 && yy < 90 && yy >-90 ) {
123

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

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

  
131

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

  
146

  
147
/** getUsage -- function that prints to stdout the usage for this driver */
148
void getUsage()
149
{
150
  cout<<"usage: metacat2shape metacat_export.txt test.shp
151

  
152
    where:
153
            input -- is an input ASCII file, that contains the following cols:
154
             -> docid -- the document id
155
             -> x -- the xlocation
156
             -> y -- the y location
157
             -> url -- the url to the document
158

  
159
            shapefile -- {output} is a ESRI shapefile
160

  
161
    see:
162
    example: metacat2shape metacat_export.txt test.shp
163

  
164
    ##><><><><><><><><><><><><><><><><><><><><><><><><>##
165
    ##           John Harris                           ##
166
    ##           harris@nceas.ucsb.edu                 ##
167
    ##           Copyright 1999-2003                   ##
168
    ##><><><><><><><><><><><><><><><><><><><><><><><><>##
169
    "<<endl;
170

  
171
}
172

  
173

  
174

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

  
184
  //these arrays are for the input x, y, z data
185
  float gridx[1000] [100];
186
  float gridy[1000] [100];
187
  float gridz[1000] [100];
188
  vector<string> docidvec;
189
  vector<string> urlvec;
190

  
191
  FILE * infile;
192
  char oneline[200];
193
  char * value;
194

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

  
202
  SHPHandle hSHP;
203
  int nShapeType, nEntities, i, iPart;
204
  const char * pszPlus;
205

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

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

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

  
226
    gridx[2] [cnt] = atof( buf );
227
    gridy[2] [cnt] = atof( buf1 );
228
    gridz[2] [cnt] = atof( buf2 );
229
    //char const * sLine = buf2;
230
    urlvec.push_back(buf2);
231
    docidvec.push_back(bufa);
232
    cnt++;
233
    number_lines = cnt;
234
  }
235
  fclose( infile );
236

  
237
  cout<<"creating a new shape file, with "<< number_lines <<" records "<<endl;
238
  WritePointsArray( SHPT_POINT, out_shape, gridx, gridy, gridz,
239
                    urlvec, docidvec, number_lines );
240

  
241
#ifdef USE_DBMALLOC
242
  malloc_dump( 2 );
243
#endif
244
  exit( 0 );
245
}
0 246

  

Also available in: Unified diff