Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: GDAL 4 : * Purpose: Artificial Intelligence powered driver 5 : * Author: Even Rouault, <even dot rouault at spatialys.com> 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2024, Even Rouault <even dot rouault at spatialys.com> 9 : * 10 : * SPDX-License-Identifier: MIT 11 : ****************************************************************************/ 12 : 13 : #include "ogrsf_frmts.h" 14 : 15 : /************************************************************************/ 16 : /* OGRAIVectorIdentify() */ 17 : /************************************************************************/ 18 : 19 42749 : static int OGRAIVectorIdentify(GDALOpenInfo *poOpenInfo) 20 : { 21 85496 : return STARTS_WITH_CI(poOpenInfo->pszFilename, "AIVector:") || 22 85496 : poOpenInfo->IsSingleAllowedDriver("AIVector"); 23 : } 24 : 25 : /************************************************************************/ 26 : /* OGRAIVectorOpen() */ 27 : /************************************************************************/ 28 : 29 1 : static GDALDataset *OGRAIVectorOpen(GDALOpenInfo *poOpenInfo) 30 : { 31 1 : if (!OGRAIVectorIdentify(poOpenInfo)) 32 0 : return nullptr; 33 : 34 : class MyLayer final : public OGRLayer, 35 : public OGRGetNextFeatureThroughRaw<MyLayer> 36 : { 37 : OGRFeatureDefn *m_poLayerDefn = nullptr; 38 : bool m_bReturnedFeature = false; 39 : 40 : CPL_DISALLOW_COPY_ASSIGN(MyLayer) 41 : 42 : public: 43 1 : MyLayer() 44 1 : { 45 1 : m_poLayerDefn = new OGRFeatureDefn("result"); 46 1 : SetDescription(m_poLayerDefn->GetName()); 47 1 : m_poLayerDefn->Reference(); 48 2 : OGRFieldDefn oFieldDefn("name", OFTString); 49 1 : m_poLayerDefn->AddFieldDefn(&oFieldDefn); 50 : OGRSpatialReference *poSRS = new OGRSpatialReference( 51 : "GEOGCS[\"I don't know\",\n" 52 : " DATUM[\"I don't care\",\n" 53 : " SPHEROID[\"GRS 1980\",6378137,298.257222101,\n" 54 : " AUTHORITY[\"EPSG\",\"7019\"]]],\n" 55 : " PRIMEM[\"Greenwich\",0,\n" 56 : " AUTHORITY[\"EPSG\",\"8901\"]],\n" 57 : " UNIT[\"degree\",0.0174532925199433,\n" 58 : " AUTHORITY[\"EPSG\",\"9122\"]],\n" 59 1 : " AUTHORITY[\"AI\",\"TOTALLY_MADE_UP\"]]"); 60 1 : m_poLayerDefn->GetGeomFieldDefn(0)->SetSpatialRef(poSRS); 61 1 : poSRS->Release(); 62 1 : } 63 : 64 1 : ~MyLayer() override 65 1 : { 66 1 : m_poLayerDefn->Release(); 67 1 : } 68 : 69 114 : void ResetReading() override 70 : { 71 114 : m_bReturnedFeature = false; 72 114 : } 73 : 74 281 : OGRFeatureDefn *GetLayerDefn() override 75 : { 76 281 : return m_poLayerDefn; 77 : } 78 80 : DEFINE_GET_NEXT_FEATURE_THROUGH_RAW(MyLayer) 79 : 80 91 : OGRFeature *GetNextRawFeature() 81 : { 82 91 : if (m_bReturnedFeature) 83 40 : return nullptr; 84 51 : m_bReturnedFeature = true; 85 51 : OGRFeature *poFeature = new OGRFeature(m_poLayerDefn); 86 51 : poFeature->SetFID(0); 87 51 : poFeature->SetField(0, "Null Island: the place to be"); 88 51 : OGRPoint *poPoint = new OGRPoint(0, 0); 89 51 : poPoint->assignSpatialReference(GetSpatialRef()); 90 51 : poFeature->SetGeometryDirectly(poPoint); 91 51 : return poFeature; 92 : } 93 : 94 36 : int TestCapability(const char *) override 95 : { 96 36 : return false; 97 : } 98 : }; 99 : 100 : class MyDataset final : public GDALDataset 101 : { 102 : MyLayer m_oLayer{}; 103 : 104 : public: 105 1 : MyDataset() = default; 106 : 107 28 : int GetLayerCount() override 108 : { 109 28 : return 1; 110 : } 111 : 112 25 : OGRLayer *GetLayer(int idx) override 113 : { 114 25 : return idx == 0 ? &m_oLayer : nullptr; 115 : } 116 : }; 117 : 118 1 : return new MyDataset(); 119 : } 120 : 121 : /************************************************************************/ 122 : /* RegisterOGRAIVector() */ 123 : /************************************************************************/ 124 : 125 1595 : void RegisterOGRAIVector() 126 : { 127 1595 : if (!GDAL_CHECK_VERSION("AIVector")) 128 0 : return; 129 : 130 1595 : if (GDALGetDriverByName("AIVector") != nullptr) 131 302 : return; 132 : 133 1293 : GDALDriver *poDriver = new GDALDriver(); 134 1293 : poDriver->SetDescription("AIVector"); 135 1293 : poDriver->SetMetadataItem(GDAL_DCAP_VECTOR, "YES"); 136 1293 : poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, 137 1293 : "Artificial Intelligence powered vector driver"); 138 1293 : poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, 139 1293 : "drivers/vector/aivector.html"); 140 : 141 1293 : poDriver->SetMetadataItem(GDAL_DMD_CONNECTION_PREFIX, "AIVector:"); 142 : 143 1293 : poDriver->pfnOpen = OGRAIVectorOpen; 144 1293 : poDriver->pfnIdentify = OGRAIVectorIdentify; 145 1293 : GetGDALDriverManager()->RegisterDriver(poDriver); 146 : }