| 92 | |
| 93 | |
| 94 | void OverlayFilter::ready(PointTableRef table) |
| 95 | { |
| 96 | m_ds = OGRDSPtr(OGROpen(m_datasource.c_str(), 0, 0), |
| 97 | [](OGRDSPtr::element_type *p){ if (p) ::OGR_DS_Destroy(p); }); |
| 98 | if (!m_ds) |
| 99 | throwError("Unable to open data source '" + m_datasource + "'"); |
| 100 | |
| 101 | if (m_layer.size()) |
| 102 | m_lyr = OGR_DS_GetLayerByName(m_ds.get(), m_layer.c_str()); |
| 103 | else if (m_query.size()) |
| 104 | m_lyr = OGR_DS_ExecuteSQL(m_ds.get(), m_query.c_str(), 0, 0); |
| 105 | else |
| 106 | m_lyr = OGR_DS_GetLayer(m_ds.get(), 0); |
| 107 | |
| 108 | if (!m_lyr) |
| 109 | throwError("Unable to select layer '" + m_layer + "'"); |
| 110 | |
| 111 | |
| 112 | if (!m_bounds.empty()) |
| 113 | { |
| 114 | pdal::Polygon g(m_bounds.toWKT()); |
| 115 | OGR_L_SetSpatialFilter(m_lyr, g.getOGRHandle()); |
| 116 | } |
| 117 | |
| 118 | auto featureDeleter = [](OGRFeaturePtr::element_type *p) |
| 119 | { |
| 120 | if (p) |
| 121 | ::OGR_F_Destroy(p); |
| 122 | }; |
| 123 | OGRFeaturePtr feature = OGRFeaturePtr(OGR_L_GetNextFeature(m_lyr), |
| 124 | featureDeleter); |
| 125 | |
| 126 | int field_index(1); // default to first column if nothing was set |
| 127 | if (m_column.size()) |
| 128 | { |
| 129 | field_index = OGR_F_GetFieldIndex(feature.get(), m_column.c_str()); |
| 130 | if (field_index == -1) |
| 131 | throwError("No column name '" + m_column + "' was found."); |
| 132 | } |
| 133 | |
| 134 | gdal::SpatialRef sref; |
| 135 | sref.setFromLayer(m_lyr); |
| 136 | SpatialReference layerSrs(sref.wkt()); |
| 137 | |
| 138 | do |
| 139 | { |
| 140 | OGRGeometryH geom = OGR_F_GetGeometryRef(feature.get()); |
| 141 | int32_t fieldVal = OGR_F_GetFieldAsInteger(feature.get(), field_index); |
| 142 | |
| 143 | m_polygons.push_back( |
| 144 | { Polygon(geom, layerSrs), fieldVal} ); |
| 145 | |
| 146 | feature = OGRFeaturePtr(OGR_L_GetNextFeature(m_lyr), featureDeleter); |
| 147 | } |
| 148 | while (feature); |
| 149 | |
| 150 | // Initialise m_grids, otherwise this will lead to a race condition when |
| 151 | // using threading. |