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 name = layerNames[0];
110 OGRLayer *p_layer = p_vect->
getLayer(name);
113 for (
const auto& p_feature : *p_layer) {
114 OGRGeometry *p_geometry = p_feature->GetGeometryRef();
115 switch (wkbFlatten(p_geometry->getGeometryType())) {
116 case OGRwkbGeometryType::wkbPoint: {
117 OGRPoint *p_point = p_geometry->toPoint();
119 this->samples.emplace(index, *p_point);
123 xCoords.push_back(p_point->getX());
124 yCoords.push_back(p_point->getY());
129 case OGRwkbGeometryType::wkbMultiPoint: {
130 for (
const auto& p_point : *p_geometry->toMultiPoint()) {
132 this->samples.emplace(index, *p_point);
136 xCoords.push_back(p_point->getX());
137 yCoords.push_back(p_point->getY());
144 throw std::runtime_error(
"the file containing existing sample points must have only Point or MultiPoint geometries.");