pktools  2.6.7
Processing Kernel for geospatial data
ImgWriterOgr.h
1 /**********************************************************************
2 ImgWriterOgr.h: class to write vector files using OGR API library
3 Copyright (C) 2008-2012 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 #ifndef _IMGWRITEROGR_H_
21 #define _IMGWRITEROGR_H_
22 
23 #include <assert.h>
24 #include <fstream>
25 #include <string>
26 #include <sstream>
27 #include <map>
28 #include <vector>
29 #include "ogrsf_frmts.h"
30 #include "ImgReaderGdal.h"
31 #include "ImgWriterGdal.h"
32 #include "ImgReaderOgr.h"
33 
34 //--------------------------------------------------------------------------
36 {
37 public:
38  ImgWriterOgr(void);
39  ImgWriterOgr(const std::string& filename, const std::string& imageType="ESRI Shapefile");
40  ImgWriterOgr(const std::string& filename, ImgReaderOgr& imgReaderOgr);
41  ImgWriterOgr(const std::string& filename, ImgReaderOgr& imgReaderOgr, bool copyData);
42  ~ImgWriterOgr(void);
43  void open(const std::string& filename, ImgReaderOgr& imgReaderOgr);
44  void open(const std::string& filename, const std::string& imageType="ESRI Shapefile");
45  void close(void);
46  int ascii2ogr(const std::string& filename, const std::string &layername, const std::vector<std::string>& fieldName, const std::vector<OGRFieldType>& fieldType, short colX=1, short colY=2, const std::string& theProjection="", const OGRwkbGeometryType& eGType=wkbPoint, const char fs=' ');
47  OGRLayer* createLayer(const std::string& layername="New layer", const std::string& theProjection="", const OGRwkbGeometryType& eGType=wkbUnknown, char** papszOptions=NULL);
48  OGRLayer* copyLayer(OGRLayer* poSrcLayer, const std::string& layername, char** papszOptions=NULL);
49  void createField(const std::string& fieldname, const OGRFieldType& fieldType, int theLayer=0);//default: get back layer
50  OGRLayer* getLayer(int layer=0) const {return m_datasource->GetLayer(layer);};
51  std::string getLayerName(int layer=0){return m_datasource->GetLayer(layer)->GetLayerDefn()->GetName();};
52  int getFields(std::vector<std::string>& fields, int layer=0) const;
53  int getFields(std::vector<OGRFieldDefn*>& fields, int layer=0) const;
54  void copyFields(const ImgReaderOgr& imgReaderOgr, int srcLayer=0, int targetLayer=0);//default: get back layer
55  void addLineString(std::vector<OGRPoint*>& points, const std::string& fieldName, const std::string& theId, int layer=0);
56  void addRing(std::vector<OGRPoint*>& points, const std::string& fieldName, int theId, int layer=0);
57  void addLineString(std::vector<OGRPoint*>& points, const std::string& fieldName, int theId, int layer=0);
58  void addPoint(double x, double y, const std::map<std::string,double>& pointAttributes, std::string fieldName, const std::string& theId, int layer=0);
59  void addPoint(double x, double y, const std::map<std::string,double>& pointAttributes, std::string fieldName, int theId, int layer=0);
60  int addData(ImgReaderGdal& imgReader, int layer=0, bool verbose=false);
61  OGRFeature* createFeature(int layer=0);
62  OGRErr createFeature(OGRFeature* theFeature, int layer=0);
63  int getFieldCount(int layer=0) const;
64  int getFeatureCount(int layer=0) const;
65 #if GDAL_VERSION_MAJOR < 2
66  OGRDataSource* getDataSource(void) {return m_datasource;};
67  OGRSFDriver* getDriver(void) const {return m_datasource->GetDriver();};
68 #else
69  GDALDataset* getDataSource(void) {return m_datasource;};
70  GDALDriver* getDriver(void) const {return m_datasource->GetDriver();};
71 #endif
72 
73 protected:
74  void setCodec(const std::string& imageType);
75 #if GDAL_VERSION_MAJOR < 2
76  void setCodec(OGRSFDriver *poDriver);
77 #else
78  void setCodec(GDALDriver *poDriver);
79 #endif
80 
81 // OGRLayer* getLayer(int layer=0);
82 
83  std::string m_filename;
84 #if GDAL_VERSION_MAJOR < 2
85  OGRDataSource *m_datasource;
86 #else
87  GDALDataset *m_datasource;
88 #endif
89 // vector<OGRLayer*> m_layers;
90 };
91 
92 #endif // _IMGWRITEROGR_H_