Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: Carto Translator 4 : * Purpose: Implements OGRCARTOResultLayer class. 5 : * Author: Even Rouault, <even dot rouault at spatialys.com> 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2013, Even Rouault <even dot rouault at spatialys.com> 9 : * 10 : * Permission is hereby granted, free of charge, to any person obtaining a 11 : * copy of this software and associated documentation files (the "Software"), 12 : * to deal in the Software without restriction, including without limitation 13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 : * and/or sell copies of the Software, and to permit persons to whom the 15 : * Software is furnished to do so, subject to the following conditions: 16 : * 17 : * The above copyright notice and this permission notice shall be included 18 : * in all copies or substantial portions of the Software. 19 : * 20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 : * DEALINGS IN THE SOFTWARE. 27 : ****************************************************************************/ 28 : 29 : #include "ogr_carto.h" 30 : 31 : /************************************************************************/ 32 : /* OGRCARTOResultLayer() */ 33 : /************************************************************************/ 34 : 35 80 : OGRCARTOResultLayer::OGRCARTOResultLayer(OGRCARTODataSource *poDSIn, 36 80 : const char *pszRawQueryIn) 37 80 : : OGRCARTOLayer(poDSIn), poFirstFeature(nullptr) 38 : { 39 80 : osBaseSQL = pszRawQueryIn; 40 80 : SetDescription("result"); 41 80 : } 42 : 43 : /************************************************************************/ 44 : /* ~OGRCARTOResultLayer() */ 45 : /************************************************************************/ 46 : 47 160 : OGRCARTOResultLayer::~OGRCARTOResultLayer() 48 : 49 : { 50 80 : delete poFirstFeature; 51 160 : } 52 : 53 : /************************************************************************/ 54 : /* GetLayerDefnInternal() */ 55 : /************************************************************************/ 56 : 57 168 : OGRFeatureDefn *OGRCARTOResultLayer::GetLayerDefnInternal(json_object *poObjIn) 58 : { 59 168 : if (poFeatureDefn != nullptr) 60 100 : return poFeatureDefn; 61 : 62 68 : EstablishLayerDefn("result", poObjIn); 63 : 64 68 : return poFeatureDefn; 65 : } 66 : 67 : /************************************************************************/ 68 : /* GetNextRawFeature() */ 69 : /************************************************************************/ 70 : 71 260 : OGRFeature *OGRCARTOResultLayer::GetNextRawFeature() 72 : { 73 260 : if (poFirstFeature) 74 : { 75 58 : OGRFeature *poRet = poFirstFeature; 76 58 : poFirstFeature = nullptr; 77 58 : return poRet; 78 : } 79 : else 80 202 : return OGRCARTOLayer::GetNextRawFeature(); 81 : } 82 : 83 : /************************************************************************/ 84 : /* IsOK() */ 85 : /************************************************************************/ 86 : 87 80 : bool OGRCARTOResultLayer::IsOK() 88 : { 89 80 : CPLErrorReset(); 90 80 : poFirstFeature = GetNextFeature(); 91 80 : return CPLGetLastErrorType() == 0; 92 : } 93 : 94 : /************************************************************************/ 95 : /* GetSRS_SQL() */ 96 : /************************************************************************/ 97 : 98 0 : CPLString OGRCARTOResultLayer::GetSRS_SQL(const char *pszGeomCol) 99 : { 100 0 : CPLString osSQL; 101 0 : CPLString osLimitedSQL; 102 : 103 0 : size_t nPos = osBaseSQL.ifind(" LIMIT "); 104 0 : if (nPos != std::string::npos) 105 : { 106 0 : osLimitedSQL = osBaseSQL; 107 0 : size_t nSize = osLimitedSQL.size(); 108 0 : for (size_t i = nPos + strlen(" LIMIT "); i < nSize; i++) 109 : { 110 0 : if (osLimitedSQL[i] == ' ' && osLimitedSQL[i - 1] == '0') 111 : { 112 0 : osLimitedSQL[i - 1] = '1'; 113 0 : break; 114 : } 115 0 : osLimitedSQL[i] = '0'; 116 : } 117 : } 118 : else 119 0 : osLimitedSQL.Printf("%s LIMIT 1", osBaseSQL.c_str()); 120 : 121 : /* Assuming that the SRID of the first non-NULL geometry applies */ 122 : /* to geometries of all rows. */ 123 : osSQL.Printf("SELECT srid, srtext FROM spatial_ref_sys WHERE srid IN " 124 : "(SELECT ST_SRID(%s) FROM (%s) ogr_subselect)", 125 0 : OGRCARTOEscapeIdentifier(pszGeomCol).c_str(), 126 0 : osLimitedSQL.c_str()); 127 : 128 0 : return osSQL; 129 : }