1
#include "graticulecreator.h"
3
#include <qtextstream.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
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,
33
DBFClose( myDbfHandle );
34
SHPClose( myShapeHandle );
38
/* DbfName need not include the file extension. */
39
DBFHandle GraticuleCreator::createDbf (QString theDbfName )
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
46
myDbfHandle = DBFCreate( myBaseString+".dbf" );
47
//create an index field named after the base part of the file name
49
DBFAddField( myDbfHandle, myBaseString+"_id", FTInteger, 11, 0 );
50
//create a second arbitary attribute field
51
DBFAddField( myDbfHandle, "Date", FTString, 12, 0 );
53
DBFClose( myDbfHandle );
55
myDbfHandle = DBFOpen( myBaseString+".dbf", "r+b" );
60
SHPHandle GraticuleCreator::createShapeFile(QString theFileName )
62
SHPHandle myShapeHandle;
63
//myShapeHandle = SHPCreate(theFileName, SHPT_POINT );
64
myShapeHandle = SHPCreate(theFileName, SHPT_ARC );
68
void GraticuleCreator::writeDbfRecord (DBFHandle theDbfHandle, int theRecordIdInt, QString theLabel)
72
std::cerr << "writeDbfRecord : " << theRecordIdInt << " - " << theLabel;
73
if (! DBFWriteIntegerAttribute(theDbfHandle, theRecordIdInt, 0, theRecordIdInt))
75
std::cerr << "DBFWriteIntegerAttribute failed. : " << theRecordIdInt << " - " << theRecordIdInt << std::endl;
77
//exit(ERR_DBFWRITEINTEGERATTRIBUTE);
81
if (! DBFWriteStringAttribute(theDbfHandle, theRecordIdInt, 1, theLabel))
83
std::cerr << "DBFWriteStringAttribute failed. : " << theRecordIdInt << " - " << theLabel <<std::endl;
84
//exit(ERR_DBFWRITEINTEGERATTRIBUTE);
86
std::cerr << " - OK! " << std::endl;
88
//DBFWriteIntegerAttribute(theDbfHandle, theRecordIdInt, 0, theRecordIdInt);
89
//DBFWriteStringAttribute(theDbfHandle, theRecordIdInt, 1, theLabel);
92
void GraticuleCreator::writePoint(SHPHandle theShapeHandle, int theRecordInt, double theXDouble, double theYDouble )
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 );
100
void GraticuleCreator::writeLine(SHPHandle theShapeHandle,
102
int theCoordinateCountInt,
103
double * theXArrayDouble,
104
double * theYArrayDouble )
106
SHPObject * myShapeObject;
107
myShapeObject = SHPCreateObject( SHPT_ARC,
112
theCoordinateCountInt,
117
SHPWriteObject( theShapeHandle, -1, myShapeObject );
118
SHPDestroyObject( myShapeObject );
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)
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));
142
for (double myXDouble = theXOriginDouble;myXDouble <=theXEndPointDouble;myXDouble+=theXIntervalDouble)
145
myXArrayDouble[0]=myXDouble;
146
myXArrayDouble[1]=myXDouble;
147
myYArrayDouble[0]=theYOriginDouble;
148
myYArrayDouble[1]=theYEndPointDouble;
150
writeDbfRecord(theDbfHandle,myRecordInt,"testing");
151
writeLine(theShapeHandle, myRecordInt, 2, myXArrayDouble, myYArrayDouble); //2=no vertices
159
for (double myYDouble=theYOriginDouble;myYDouble<=theYEndPointDouble;myYDouble+=theYIntervalDouble)
162
myXArrayDouble[0]=theXOriginDouble;
163
myXArrayDouble[1]=theXEndPointDouble;
164
myYArrayDouble[0]=myYDouble;
165
myYArrayDouble[1]=myYDouble;
167
writeDbfRecord(theDbfHandle,myRecordInt,"testing");
168
writeLine(theShapeHandle, myRecordInt, 2, myXArrayDouble, myYArrayDouble); //2=no vertices
173
delete myXArrayDouble;
174
delete myYArrayDouble;
177
/* read from fp and generate point shapefile to theDbfHandle/theShapeHandle */
178
void GraticuleCreator::generatePoints (QString theInputFileName, DBFHandle theDbfHandle, SHPHandle theShapeHandle)
180
QFile myFile( theInputFileName );
181
if ( myFile.open( IO_ReadOnly ) )
183
QTextStream myStream( &myFile );
184
QString myLineString;
186
while ( !myStream.atEnd() )
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);
193
if (myQStringList.size()==4)
195
QString myDateQString = myQStringList[1];
196
QString myLatQString = myQStringList[2];
197
QString myLongQString = myQStringList[3];
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);