Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: AmigoCloud Translator 4 : * Purpose: Implements OGRAmigoCloudResultLayer class. 5 : * Author: Victor Chernetsky, <victor at amigocloud dot com> 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2015, Victor Chernetsky, <victor at amigocloud dot 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_amigocloud.h" 30 : 31 : /************************************************************************/ 32 : /* OGRAmigoCloudResultLayer() */ 33 : /************************************************************************/ 34 : 35 0 : OGRAmigoCloudResultLayer::OGRAmigoCloudResultLayer( 36 0 : OGRAmigoCloudDataSource *poDSIn, const char *pszRawQueryIn) 37 0 : : OGRAmigoCloudLayer(poDSIn) 38 : { 39 0 : osBaseSQL = pszRawQueryIn; 40 0 : SetDescription("result"); 41 0 : poFirstFeature = nullptr; 42 0 : } 43 : 44 : /************************************************************************/ 45 : /* ~OGRAmigoCloudResultLayer() */ 46 : /************************************************************************/ 47 : 48 0 : OGRAmigoCloudResultLayer::~OGRAmigoCloudResultLayer() 49 : 50 : { 51 0 : delete poFirstFeature; 52 0 : } 53 : 54 : /************************************************************************/ 55 : /* GetLayerDefnInternal() */ 56 : /************************************************************************/ 57 : 58 : OGRFeatureDefn * 59 0 : OGRAmigoCloudResultLayer::GetLayerDefnInternal(json_object *poObjIn) 60 : { 61 0 : if (poFeatureDefn != nullptr) 62 0 : return poFeatureDefn; 63 : 64 0 : EstablishLayerDefn("result", poObjIn); 65 : 66 0 : return poFeatureDefn; 67 : } 68 : 69 : /************************************************************************/ 70 : /* GetNextRawFeature() */ 71 : /************************************************************************/ 72 : 73 0 : OGRFeature *OGRAmigoCloudResultLayer::GetNextRawFeature() 74 : { 75 0 : if (poFirstFeature) 76 : { 77 0 : OGRFeature *poRet = poFirstFeature; 78 0 : poFirstFeature = nullptr; 79 0 : return poRet; 80 : } 81 : else 82 0 : return OGRAmigoCloudLayer::GetNextRawFeature(); 83 : } 84 : 85 : /************************************************************************/ 86 : /* IsOK() */ 87 : /************************************************************************/ 88 : 89 0 : int OGRAmigoCloudResultLayer::IsOK() 90 : { 91 0 : CPLErrorReset(); 92 0 : poFirstFeature = GetNextFeature(); 93 0 : return CPLGetLastErrorType() == 0; 94 : } 95 : 96 : /************************************************************************/ 97 : /* GetSRS_SQL() */ 98 : /************************************************************************/ 99 : 100 0 : CPLString OGRAmigoCloudResultLayer::GetSRS_SQL(const char *pszGeomCol) 101 : { 102 0 : CPLString osSQL; 103 0 : CPLString osLimitedSQL; 104 : 105 0 : size_t nPos = osBaseSQL.ifind(" LIMIT "); 106 0 : if (nPos != std::string::npos) 107 : { 108 0 : osLimitedSQL = osBaseSQL; 109 0 : size_t nSize = osLimitedSQL.size(); 110 0 : for (size_t i = nPos + strlen(" LIMIT "); i < nSize; i++) 111 : { 112 0 : if (osLimitedSQL[i] == ' ' && osLimitedSQL[i - 1] == '0') 113 : { 114 0 : osLimitedSQL[i - 1] = '1'; 115 0 : break; 116 : } 117 0 : osLimitedSQL[i] = '0'; 118 : } 119 : } 120 : else 121 0 : osLimitedSQL.Printf("%s LIMIT 1", osBaseSQL.c_str()); 122 : 123 : /* Assuming that the SRID of the first non-NULL geometry applies */ 124 : /* to geometries of all rows. */ 125 : osSQL.Printf("SELECT srid, srtext FROM spatial_ref_sys WHERE srid IN " 126 : "(SELECT ST_SRID(%s) FROM (%s) ogr_subselect)", 127 0 : OGRAMIGOCLOUDEscapeIdentifier(pszGeomCol).c_str(), 128 0 : osLimitedSQL.c_str()); 129 : 130 0 : return osSQL; 131 : }