~ubuntu-branches/ubuntu/quantal/qgis/quantal

« back to all changes in this revision

Viewing changes to plugins/grid_maker/graticulecreator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve Halasz
  • Date: 2004-12-21 09:46:27 UTC
  • Revision ID: james.westby@ubuntu.com-20041221094627-r9lb6mlz2o3yp8gn
Tags: upstream-0.6.0
ImportĀ upstreamĀ versionĀ 0.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "graticulecreator.h"
 
2
#include <stdio.h>
 
3
#include <qtextstream.h>
 
4
#include <iostream>
 
5
#include <qfileinfo.h>
 
6
#include <qstringlist.h>
 
7
GraticuleCreator::GraticuleCreator(QString theOutputFileName, 
 
8
                                   double theXIntervalDouble, 
 
9
                                   double theYIntervalDouble,
 
10
                                   double theXOriginDouble,
 
11
                                   double theYOriginDouble,
 
12
                                   double theXEndPointDouble,
 
13
                                   double theYEndPointDouble
 
14
                                   )
 
15
{
 
16
    std::cout << "GraticuleCreator constructor called with " << theOutputFileName
 
17
    << " for output file and " << theXIntervalDouble << "," << theYIntervalDouble << " for x,y interval " << std::endl;
 
18
    DBFHandle myDbfHandle;              /* handle for dBase file */
 
19
    SHPHandle myShapeHandle;            /* handle for shape files .shx and .shp */
 
20
    /* Open and prepare output files */
 
21
    myDbfHandle = createDbf(theOutputFileName);
 
22
    myShapeHandle = createShapeFile(theOutputFileName);
 
23
    //test the write point routine....
 
24
    //generatePoints(theInputFileName,myDbfHandle,myShapeHandle);
 
25
    generateGraticule(myDbfHandle,
 
26
                      myShapeHandle,
 
27
                      theXIntervalDouble,
 
28
                      theYIntervalDouble,
 
29
                      theXOriginDouble,
 
30
                      theYOriginDouble,
 
31
                      theXEndPointDouble,
 
32
                      theYEndPointDouble);
 
33
    DBFClose( myDbfHandle );
 
34
    SHPClose( myShapeHandle );
 
35
    return;
 
36
}
 
37
 
 
38
/* DbfName need not include the file extension. */
 
39
DBFHandle GraticuleCreator::createDbf (QString theDbfName )
 
40
{
 
41
    DBFHandle myDbfHandle;
 
42
    //remove the path part of the dbf name
 
43
    QFileInfo myFileInfo( theDbfName );
 
44
    QString myBaseString = myFileInfo.dirPath()+QString("/")+myFileInfo.baseName();  // excludes any extension
 
45
    //create the dbf
 
46
    myDbfHandle = DBFCreate( myBaseString+".dbf" );
 
47
    //create an index field named after the base part of the file name
 
48
 
 
49
    DBFAddField( myDbfHandle, myBaseString+"_id", FTInteger, 11, 0 );
 
50
    //create a second arbitary attribute field
 
51
    DBFAddField( myDbfHandle, "Date", FTString, 12, 0 );
 
52
    //close the dbf
 
53
    DBFClose( myDbfHandle );
 
54
    //reopen
 
55
    myDbfHandle = DBFOpen( myBaseString+".dbf", "r+b" );
 
56
    //exit this fn giving
 
57
    return myDbfHandle;
 
58
}
 
59
 
 
60
SHPHandle GraticuleCreator::createShapeFile(QString theFileName )
 
61
{
 
62
    SHPHandle myShapeHandle;
 
63
    //myShapeHandle = SHPCreate(theFileName, SHPT_POINT );
 
64
    myShapeHandle = SHPCreate(theFileName, SHPT_ARC );
 
65
    return myShapeHandle;
 
66
}
 
67
 
 
68
void GraticuleCreator::writeDbfRecord (DBFHandle theDbfHandle, int theRecordIdInt, QString theLabel)
 
69
{
 
70
 
 
71
  
 
72
    std::cerr << "writeDbfRecord : " << theRecordIdInt << " - " << theLabel;
 
73
    if (! DBFWriteIntegerAttribute(theDbfHandle, theRecordIdInt, 0, theRecordIdInt))
 
74
    {
 
75
        std::cerr <<  "DBFWriteIntegerAttribute failed. : " <<  theRecordIdInt << " - " << theRecordIdInt << std::endl;
 
76
 
 
77
        //exit(ERR_DBFWRITEINTEGERATTRIBUTE);
 
78
    }
 
79
    if (theLabel != NULL)
 
80
    {
 
81
      if (! DBFWriteStringAttribute(theDbfHandle, theRecordIdInt, 1, theLabel))
 
82
      {
 
83
        std::cerr <<  "DBFWriteStringAttribute failed. : " <<  theRecordIdInt << " - " << theLabel <<std::endl;
 
84
        //exit(ERR_DBFWRITEINTEGERATTRIBUTE);
 
85
      }
 
86
      std::cerr << " - OK! " << std::endl;
 
87
    }
 
88
    //DBFWriteIntegerAttribute(theDbfHandle, theRecordIdInt, 0, theRecordIdInt);
 
89
    //DBFWriteStringAttribute(theDbfHandle, theRecordIdInt, 1, theLabel);
 
90
}
 
91
 
 
92
void GraticuleCreator::writePoint(SHPHandle theShapeHandle, int theRecordInt, double theXDouble, double theYDouble )
 
93
{
 
94
    SHPObject * myShapeObject;
 
95
    myShapeObject = SHPCreateObject( SHPT_POINT, theRecordInt, 0, NULL, NULL, 1, &theXDouble, &theYDouble, NULL, NULL );
 
96
    SHPWriteObject( theShapeHandle, -1, myShapeObject );
 
97
    SHPDestroyObject( myShapeObject );
 
98
}
 
99
 
 
100
void GraticuleCreator::writeLine(SHPHandle theShapeHandle, 
 
101
        int theRecordInt, 
 
102
        int theCoordinateCountInt, 
 
103
        double * theXArrayDouble, 
 
104
        double * theYArrayDouble ) 
 
105
{
 
106
  SHPObject * myShapeObject;
 
107
  myShapeObject = SHPCreateObject( SHPT_ARC, 
 
108
                                   theRecordInt, 
 
109
                                   0, 
 
110
                                   NULL, 
 
111
                                   NULL,
 
112
                                   theCoordinateCountInt, 
 
113
                                   theXArrayDouble, 
 
114
                                   theYArrayDouble,
 
115
                                   NULL, 
 
116
                                   NULL );
 
117
  SHPWriteObject( theShapeHandle, -1, myShapeObject );
 
118
  SHPDestroyObject( myShapeObject );
 
119
}
 
120
 
 
121
//TODO: check for rediculous intervals!
 
122
void GraticuleCreator::generateGraticule(DBFHandle theDbfHandle, 
 
123
                                         SHPHandle theShapeHandle,
 
124
                                         double theXIntervalDouble,
 
125
                                         double theYIntervalDouble,
 
126
                                         double theXOriginDouble,
 
127
                                         double theYOriginDouble,
 
128
                                         double theXEndPointDouble,
 
129
                                         double theYEndPointDouble)
 
130
{
 
131
  
 
132
  int myRecordInt=0;
 
133
  //create the arrays for storing the coordinates
 
134
  double * myXArrayDouble;
 
135
  double * myYArrayDouble;
 
136
  myXArrayDouble = (double *)malloc(2 * sizeof(double)); //2=no vertices
 
137
  myYArrayDouble = (double *)malloc(2 * sizeof(double));
 
138
  
 
139
  //
 
140
  //Longitude loop
 
141
  //
 
142
  for (double myXDouble = theXOriginDouble;myXDouble <=theXEndPointDouble;myXDouble+=theXIntervalDouble)
 
143
  {
 
144
    
 
145
    myXArrayDouble[0]=myXDouble;
 
146
    myXArrayDouble[1]=myXDouble;
 
147
    myYArrayDouble[0]=theYOriginDouble;
 
148
    myYArrayDouble[1]=theYEndPointDouble;
 
149
 
 
150
    writeDbfRecord(theDbfHandle,myRecordInt,"testing");
 
151
    writeLine(theShapeHandle, myRecordInt, 2, myXArrayDouble, myYArrayDouble); //2=no vertices
 
152
    
 
153
    ++myRecordInt;
 
154
  }
 
155
 
 
156
  //
 
157
  //Latitude loop
 
158
  //
 
159
  for (double myYDouble=theYOriginDouble;myYDouble<=theYEndPointDouble;myYDouble+=theYIntervalDouble)
 
160
  {
 
161
    
 
162
    myXArrayDouble[0]=theXOriginDouble;
 
163
    myXArrayDouble[1]=theXEndPointDouble;
 
164
    myYArrayDouble[0]=myYDouble;
 
165
    myYArrayDouble[1]=myYDouble;
 
166
    
 
167
    writeDbfRecord(theDbfHandle,myRecordInt,"testing");
 
168
    writeLine(theShapeHandle, myRecordInt, 2, myXArrayDouble, myYArrayDouble); //2=no vertices
 
169
 
 
170
    ++myRecordInt;
 
171
  }
 
172
  
 
173
  delete myXArrayDouble;
 
174
  delete myYArrayDouble;
 
175
}
 
176
 
 
177
/* read from fp and generate point shapefile to theDbfHandle/theShapeHandle */
 
178
void GraticuleCreator::generatePoints (QString theInputFileName, DBFHandle theDbfHandle, SHPHandle theShapeHandle)
 
179
{
 
180
    QFile myFile( theInputFileName );
 
181
    if ( myFile.open( IO_ReadOnly ) )
 
182
    {
 
183
        QTextStream myStream( &myFile );
 
184
        QString myLineString;
 
185
        int myRecordInt = 0;
 
186
        while ( !myStream.atEnd() )
 
187
        {
 
188
            // line of text excluding '\n'
 
189
            myLineString = myStream.readLine();
 
190
            //tokenise the line so we can get coords and records
 
191
            QStringList myQStringList = QStringList::split("\t",myLineString,true);
 
192
 
 
193
            if (myQStringList.size()==4)
 
194
            {
 
195
                QString myDateQString = myQStringList[1];
 
196
                QString myLatQString = myQStringList[2];
 
197
                QString myLongQString = myQStringList[3];
 
198
 
 
199
                //convert items 3 and 4 to lat and long...
 
200
                //TODO - continue here...
 
201
                double x=myLongQString.toDouble();
 
202
                double y=myLatQString.toDouble();
 
203
                //create the dbf and shape recs
 
204
                std::cerr << "Writing record: " << myDateQString << " - " << x << " - " << y << std::endl;
 
205
                writeDbfRecord(theDbfHandle, myRecordInt, myDateQString);
 
206
                writePoint(theShapeHandle, myRecordInt, x, y);
 
207
                myRecordInt++;
 
208
            }
 
209
        }
 
210
        myFile.close();
 
211
    }
 
212
}
 
213
 
 
214
 
 
215
 
 
216