1 /****************************************************************************** 2 * $Id: kml.h b1c9c12ad373e40b955162b45d704070d4ebf7b0 2019-06-19 16:50:15 +0200 Even Rouault $ 3 * 4 * Project: KML Driver 5 * Purpose: Class for reading, parsing and handling a kmlfile. 6 * Author: Jens Oberender, j.obi@troja.net 7 * 8 ****************************************************************************** 9 * Copyright (c) 2007, Jens Oberender 10 * Copyright (c) 2008-2012, Even Rouault <even dot rouault at spatialys.com> 11 * 12 * Permission is hereby granted, free of charge, to any person obtaining a 13 * copy of this software and associated documentation files (the "Software"), 14 * to deal in the Software without restriction, including without limitation 15 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 16 * and/or sell copies of the Software, and to permit persons to whom the 17 * Software is furnished to do so, subject to the following conditions: 18 * 19 * The above copyright notice and this permission notice shall be included 20 * in all copies or substantial portions of the Software. 21 * 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 28 * DEALINGS IN THE SOFTWARE. 29 ****************************************************************************/ 30 #ifndef OGR_KML_KML_H_INCLUDED 31 #define OGR_KML_KML_H_INCLUDED 32 33 #ifdef HAVE_EXPAT 34 35 #include "ogr_expat.h" 36 #include "cpl_vsi.h" 37 38 // std 39 #include <iostream> 40 #include <string> 41 #include <vector> 42 43 #include "cpl_port.h" 44 #include "kmlutility.h" 45 46 class KMLNode; 47 48 typedef enum 49 { 50 KML_VALIDITY_UNKNOWN, 51 KML_VALIDITY_INVALID, 52 KML_VALIDITY_VALID 53 } OGRKMLValidity; 54 55 class KML 56 { 57 public: 58 KML(); 59 virtual ~KML(); 60 bool open(const char* pszFilename); 61 bool isValid(); 62 bool isHandled(std::string const& elem) const; 63 virtual bool isLeaf(std::string const& elem) const; 64 virtual bool isFeature(std::string const& elem) const; 65 virtual bool isFeatureContainer(std::string const& elem) const; 66 virtual bool isContainer(std::string const& elem) const; 67 virtual bool isRest(std::string const& elem) const; 68 virtual void findLayers(KMLNode* poNode, int bKeepEmptyContainers); 69 70 bool hasOnlyEmpty() const; 71 72 bool parse(); 73 void print(unsigned short what = 3); 74 std::string getError() const; 75 int classifyNodes(); 76 void eliminateEmpty(); 77 int getNumLayers() const; 78 bool selectLayer(int); 79 std::string getCurrentName() const; 80 Nodetype getCurrentType() const; 81 int is25D() const; 82 int getNumFeatures(); 83 Feature* getFeature(std::size_t nNum, int& nLastAsked, int &nLastCount); 84 85 void unregisterLayerIfMatchingThisNode(KMLNode* poNode); 86 87 protected: 88 void checkValidity(); 89 90 static void XMLCALL startElement(void *, const char *, const char **); 91 static void XMLCALL startElementValidate(void *, const char *, const char **); 92 static void XMLCALL dataHandler(void *, const char *, int); 93 static void XMLCALL dataHandlerValidate(void *, const char *, int); 94 static void XMLCALL endElement(void *, const char *); 95 96 // Trunk of KMLnodes. 97 KMLNode* poTrunk_; 98 // Number of layers. 99 int nNumLayers_; 100 KMLNode** papoLayers_; 101 102 private: 103 // Depth of the DOM. 104 unsigned int nDepth_; 105 // KML version number. 106 std::string sVersion_; 107 // Set to KML_VALIDITY_VALID if the beginning of the file is detected as KML 108 OGRKMLValidity validity; 109 // File descriptor. 110 VSILFILE *pKMLFile_; 111 // Error text ("" when everything is OK"). 112 std::string sError_; 113 // Current KMLNode. 114 KMLNode *poCurrent_; 115 116 XML_Parser oCurrentParser; 117 int nDataHandlerCounter; 118 int nWithoutEventCounter; 119 }; 120 121 #endif // HAVE_EXPAT 122 123 #endif /* OGR_KML_KML_H_INCLUDED */ 124