stuffy_stuff/shp_2_txt.cpp

68 lines
1.8 KiB
C++
Raw Normal View History

2019-05-23 21:08:28 +02:00
/*
http://shapelib.maptools.org/dbf_api.html
basic funtionality taken from dbfdump.c
path to libshp.dll has to be added as a PATH var
*/
2019-05-23 20:55:33 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;
#include "shapefil.h"
int main(int argc, char const *argv[])
2019-05-23 21:01:55 +02:00
{
DBFHandle dbfh;
int i, iRecord;
char *pszFilename = NULL;
int nWidth, nDecimals;
char szTitle[12];
string filename = "f:\\testing\\shp_2_txt\\MA.dbf";
2019-05-23 21:08:28 +02:00
// open textfile and dbf handle
2019-05-23 21:01:55 +02:00
ofstream txt_file("out.txt");
dbfh = DBFOpen(filename.c_str(), "rb");
2019-05-23 21:08:28 +02:00
// check
2019-05-23 21:01:55 +02:00
if (dbfh <= 0)
{
throw string("File " + filename + " not found");
}
else
{
2019-05-23 21:08:28 +02:00
// loop over dbf records
for( iRecord = 0; iRecord < DBFGetRecordCount(dbfh); iRecord++ )
2019-05-23 21:01:55 +02:00
{
2019-05-23 21:08:28 +02:00
// loop over fields of record
for( i = 0; i < DBFGetFieldCount(dbfh); i++ )
{
2019-05-23 21:00:42 +02:00
DBFFieldType eType;
eType = DBFGetFieldInfo( dbfh, i, szTitle, &nWidth, &nDecimals );
string dec_lat = "DEC_LAT";
string dec_lon = "DEC_LON";
string ellip_h = "ELLIP_HT";
2019-05-23 21:01:55 +02:00
string ortho_h = "ORTHO_HT";
2019-05-23 20:59:28 +02:00
string str(szTitle);
string line;
2019-05-23 20:55:33 +02:00
2019-05-23 21:08:28 +02:00
// parse for lat, lon in degrees and orhto height
2019-05-23 21:01:55 +02:00
if (szTitle == dec_lat or szTitle == dec_lon or szTitle == ortho_h)
{
txt_file << DBFReadStringAttribute( dbfh, iRecord, i );
txt_file << " ";
}
}
txt_file << endl;
}
2019-05-23 21:08:28 +02:00
// close dbf handle and textfile
2019-05-23 21:01:55 +02:00
DBFClose( dbfh );
txt_file.close();
}
2019-05-23 20:55:33 +02:00
2019-05-23 21:01:55 +02:00
return 0;
2019-05-23 20:55:33 +02:00
}