| pktools 2.6.7 Processing Kernel for geospatial data

pkdumpogr.h
1/**********************************************************************
2 pkdumpogr.h: dump ogr file to text file or standard output
3 Copyright (C) 2008-2014 Pieter Kempeneers
4
5 This file is part of pktools
6
7 pktools is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
11
12 pktools is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with pktools. If not, see <http://www.gnu.org/licenses/>.
19***********************************************************************/
20#include <map>
21#include "base/Vector2d.h"
22#include "imageclasses/ImgReaderOgr.h"
23
24#ifndef _PKDUMPOGR_H_
25#define _PKDUMPOGR_H_
26
27 using namespace std;
28
29 template<typename T> unsigned int readDataImageShape(const string &filename,
30 map<int,Vector2d<T> > &mapPixels, //[classNr][pixelNr][bandNr],
31 vector<string>& fields,
32 double start,
33 double end,
34 const string& label,
35 const string& query="",
36 int verbose=false);
37
38
39 template<typename T> unsigned int readDataImageShape(const string &filename,
40 map<int,Vector2d<T> > &mapPixels, //[classNr][pixelNr][bandNr],
41 vector<string>& fields,
42 double start,
43 double end,
44 const string& label,
45 const string& query,
46 int verbose)
47{
48 mapPixels.clear();
49 int nsample=0;
50 int totalSamples=0;
51 int nband=0;
52 if(verbose)
53 cout << "reading shape file " << filename << endl;
54 ImgReaderOgr imgReaderShape;
55 try{
56 imgReaderShape.open(filename);
57 bool queryFound=false;
58 //only retain bands in fields
59 imgReaderShape.getFields(fields);
60 vector<string>::iterator fit=fields.begin();
61 if(verbose)
62 cout << "reading fields: ";
63 while(fit!=fields.end()){
64 if(verbose)
65 cout << *fit << " ";
66 size_t pos=(*fit).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ");
67 if(pos==string::npos){
68 if(query!=""){
69 if((*fit).find(query)!=string::npos)
70 queryFound=true;
71 }
72 fields.erase(fit);
73 }
74 else{
75 string fieldname=(*fit).substr(pos);
76 int iband=atoi(fieldname.c_str());
77 if((start||end)&&(iband<start||iband>end))
78 fields.erase(fit);
79 else
80 ++fit;
81 }
82 }
83 if(verbose)
84 cout << endl;
85 if(verbose){
86 cout << "fields:";
87 for(vector<string>::iterator fit=fields.begin();fit!=fields.end();++fit)
88 cout << " " << *fit;
89 cout << endl;
90 }
91 if(!nband){
92 if(queryFound){
93 ostringstream qs;
94 qs << "select * from " << imgReaderShape.getLayerName() << " where " << query << "=1";
95 if(verbose)
96 cout << "reading with sql: " << qs.str() << endl;
97 nband=imgReaderShape.readSql(mapPixels,OFTReal,fields,label,qs.str(),NULL,0,true,false);
98 }
99 else{
100 if(verbose)
101 cout << "reading data" << endl;
102 nband=imgReaderShape.readData(mapPixels,OFTReal,fields,label,0,true,verbose==2);
103 }
104 }
105 else{
106 if(queryFound){
107 ostringstream qs;
108 qs << "select * from " << imgReaderShape.getLayerName() << " where " << query << "=1";
109 if(verbose)
110 cout << "reading with sql: " << qs.str() << endl;
111 assert(nband==imgReaderShape.readSql(mapPixels,OFTReal,fields,label,qs.str(),NULL,0,true,false));
112 }
113 else
114 assert(nband==imgReaderShape.readData(mapPixels,OFTReal,fields,label,0,true,false));
115 }
116 }
117 catch(string e){
118 ostringstream estr;
119 estr << e << " " << filename;
120 throw(estr.str());
121 }
122 nsample=imgReaderShape.getFeatureCount();
123 totalSamples+=nsample;
124 if(verbose)
125 cout << ": " << nsample << " samples read with " << nband << " bands" << endl;
126 imgReaderShape.close();
127 if(verbose)
128 cout << "total number of samples read " << totalSamples << endl;
129 return totalSamples;
130}
131#endif //_PKDUMPOGR_H_
ImgReaderOgr
Definition: ImgReaderOgr.h:36
Vector2d
Definition: Vector2d.h:32