75 std::vector<double>& xCoords,
76 std::vector<double>& yCoords,
77 bool addAllPoints =
true)
84 OGRFieldDefn existingField(
"existing", OFTInteger);
85 OGRErr err = p_samples->CreateField(&existingField);
87 throw std::runtime_error(
"cannot create 'existing' field in output layer.");
92 std::vector<std::string> layerNames = p_vect->
getLayerNames();
93 if (layerNames.size() > 1) {
94 throw std::runtime_error(
"the file containing existing sample points must have only a single layer.");
98 std::string rastProj = p_rast->
getDataset()->GetProjectionRef();
99 OGRSpatialReference rastSRS;
100 rastSRS.importFromWkt(rastProj.c_str());
101 const OGRSpatialReference *p_sampSRS = p_samples->GetSpatialRef();
102 if (!rastSRS.IsSame(p_sampSRS)) {
103 throw std::runtime_error(
"existing sample vector and raster do not have the same spatial reference system.");
107 GDALInvGeoTransform(GT, this->IGT);
109 std::string layerName = layerNames[0];
110 OGRLayer *p_layer = p_vect->
getLayer(layerName);
113 for (
const auto& p_feature : *p_layer) {
114 OGRGeometry *p_geometry = p_feature->GetGeometryRef();
116 throw std::runtime_error(std::format(
"an invalid geometry was encountered in the {} layer.", layerName));
118 switch (wkbFlatten(p_geometry->getGeometryType())) {
119 case OGRwkbGeometryType::wkbPoint: {
120 OGRPoint *p_point = p_geometry->toPoint();
122 this->samples.emplace(index, *p_point);
126 xCoords.push_back(p_point->getX());
127 yCoords.push_back(p_point->getY());
132 case OGRwkbGeometryType::wkbMultiPoint: {
133 for (
const auto& p_point : *p_geometry->toMultiPoint()) {
135 this->samples.emplace(index, *p_point);
139 xCoords.push_back(p_point->getX());
140 yCoords.push_back(p_point->getY());
147 throw std::runtime_error(
"the file containing existing sample points must have only Point or MultiPoint geometries.");