Field.cpp
Go to the documentation of this file.
1 //=============================================================================
2 // Copyright (C) 2021-2024 Wageningen University - All Rights Reserved
3 // Author: Gonzalo Mier
4 // BSD-3 License
5 //=============================================================================
6 
8 #include <regex>
9 
10 namespace f2c::types {
11 
12 Field::Field() = default;
13 Field::Field(const Cells& cells, const std::string& id_p) : id_(id_p) {
14  if (!cells.isEmpty()) {
15  this->ref_point_ = cells.getCellBorder(0).startPoint();
16  }
17  this->field_ = cells - this->ref_point_;
18 }
19 
20 Field::~Field() = default;
21 Field::Field(const Field&) = default;
22 Field::Field(Field&&) = default;
23 Field& Field::operator=(Field&&) = default;
24 Field& Field::operator=(const Field&) = default;
25 
26 
27 
28 std::string Field::getId() const {
29  return this->id_;
30 }
31 
32 void Field::setId(const std::string& _id) {
33  this->id_ = _id;
34 }
35 
36 std::string Field::getCRS() const {
37  return this->coord_sys_;
38 }
39 
40 void Field::setCRS(const std::string& crs) {
41  this->coord_sys_ = crs;
42 }
43 
44 std::string Field::getPrevCRS() const {
45  return this->prev_coord_sys_;
46 }
47 
48 void Field::setPrevCRS(const std::string& prev_crs) {
49  this->prev_coord_sys_ = prev_crs;
50 }
51 
53  return this->ref_point_;
54 }
55 
56 const Point& Field::getRefPoint() const {
57  return this->ref_point_;
58 }
59 
60 void Field::setRefPoint(const Point& _ref_point) {
61  this->ref_point_ = _ref_point;
62 }
63 
65  return this->field_;
66 }
67 
68 const Cells& Field::getField() const {
69  return this->field_;
70 }
71 
72 void Field::setField(const Cells& _field) {
73  this->field_ = _field;
74 }
75 
77  Field new_field {this->field_ + this->ref_point_, this->id_};
78  new_field.coord_sys_ = this->coord_sys_;
79  return new_field;
80 }
81 
82 double Field::area(void) const {
83  return this->field_.area();
84 }
85 
86 bool Field::isEmpty() const {
87  return this->field_.isEmpty();
88 }
89 
90 bool Field::isCoordSystemUTM(const std::string& coord_sys) {
91  return coord_sys.find("UTM:") != std::string::npos;
92 }
93 
95  return isCoordSystemUTM(this->coord_sys_);
96 }
97 
99  const std::string& coord_sys, const std::string& if_not_found) {
100  std::smatch match;
101  if (std::regex_search(coord_sys, match,
102  std::regex("UTM[^0-9A-Za-z]*(\\d++\\w)[^\\w]*"))) {
103  return match.str(1);
104  }
105  return if_not_found;
106 }
107 
108 std::string Field::getUTMDatum(
109  const std::string& coord_sys, const std::string& if_not_found) {
110  std::smatch match;
111  if (std::regex_search(coord_sys, match,
112  std::regex("datum[^0-9A-Za-z]*([0-9A-Za-z]+)"))) {
113  return match.str(1);
114  }
115  return if_not_found;
116 }
117 
118 std::string Field::getUTMCoordSystem() const {
119  return getUTMCoordSystem(this->coord_sys_);
120 }
121 
122 std::string Field::getUTMDatum() const {
123  return getUTMDatum(this->coord_sys_);
124 }
125 
126 bool Field::isCoordSystemEPSG(const std::string& coord_sys) {
127  return coord_sys.find("EPSG:") != std::string::npos;
128 }
129 
131  return isCoordSystemEPSG(this->coord_sys_);
132 }
133 
134 int Field::getEPSGCoordSystem(const std::string& coord_sys) {
135  return isCoordSystemEPSG(coord_sys) ?
136  std::stoi(coord_sys.substr(5)) : -1;
137 }
138 
140  return getEPSGCoordSystem(this->coord_sys_);
141 }
142 
144  this->coord_sys_ = "EPSG:" + std::to_string(epsg);
145 }
146 
147 void Field::setUTMCoordSystem(const std::string& utm) {
148  this->coord_sys_ =
149  "UTM:" + getUTMCoordSystem(utm, utm) +
150  " datum:" + getUTMDatum(utm);
151 }
152 
154  const std::string& utm, const std::string& datum) {
155  this->coord_sys_ =
156  "UTM:" + getUTMCoordSystem(utm, utm) +
157  " datum:" + getUTMDatum(datum, datum);
158 }
159 
161  return this->field_ + this->ref_point_;
162 }
163 
164 std::string Field::getUTMZone(const std::string& coord_sys) {
165  return getUTMCoordSystem(coord_sys).substr(0, 2);
166 }
167 
168 std::string Field::getUTMZone() const {
169  return getUTMZone(this->coord_sys_);
170 }
171 
172 std::string Field::getUTMHemisphere(const std::string& coord_sys) {
173  std::string hemisphere {getUTMCoordSystem(coord_sys).substr(2, 1)};
174  if (hemisphere == "n" || hemisphere == "N") {
175  return "+north";
176  } else if (hemisphere == "s" || hemisphere == "S") {
177  return "+south";
178  } else {
179  return "";
180  }
181 }
182 
183 std::string Field::getUTMHemisphere() const {
184  return getUTMHemisphere(this->coord_sys_);
185 }
186 
187 } // namespace f2c::types
188 
f2c::types::Geometries::area
double area() const
Compute area of the geometry.
Definition: Geometries_impl.hpp:14
1_basic_types.cells
cells
Definition: 1_basic_types.py:93
f2c::types
Types used by fields2cover library.
Definition: Cell.h:20
f2c::types::Field::operator=
Field & operator=(Field &&)
f2c::types::Field
Definition: Field.h:18
f2c::types::Field::getRefPoint
Point & getRefPoint()
Definition: Field.cpp:52
f2c::types::Field::prev_coord_sys_
std::string prev_coord_sys_
Definition: Field.h:70
f2c::types::Field::field_
Cells field_
Definition: Field.h:72
f2c::types::Cells::getCellBorder
const LinearRing getCellBorder(size_t i) const
Definition: Cells.cpp:116
f2c::types::Field::getEPSGCoordSystem
int getEPSGCoordSystem() const
Definition: Field.cpp:139
f2c::types::Field::getField
Cells & getField()
Definition: Field.cpp:64
f2c::types::Field::setEPSGCoordSystem
void setEPSGCoordSystem(int epsg)
Definition: Field.cpp:143
f2c::types::Field::ref_point_
Point ref_point_
Definition: Field.h:71
f2c::types::LinearRing::startPoint
const Point startPoint() const
Definition: LinearRing.cpp:113
f2c::types::Field::setRefPoint
void setRefPoint(const Point &_ref_point)
Definition: Field.cpp:60
f2c::types::Field::isCoordSystemEPSG
bool isCoordSystemEPSG() const
Definition: Field.cpp:130
f2c::types::Field::setCRS
void setCRS(const std::string &crs)
Definition: Field.cpp:40
f2c::types::Field::setUTMCoordSystem
void setUTMCoordSystem(const std::string &utm)
Definition: Field.cpp:147
f2c::types::Geometry::isEmpty
bool isEmpty() const
Definition: Geometry_impl.hpp:222
f2c::types::Field::getUTMCoordSystem
std::string getUTMCoordSystem() const
Definition: Field.cpp:118
f2c::types::Field::getUTMHemisphere
std::string getUTMHemisphere() const
Definition: Field.cpp:183
f2c::types::Field::isEmpty
bool isEmpty() const
Definition: Field.cpp:86
f2c::types::Field::~Field
~Field()
f2c::types::Field::area
double area() const
Definition: Field.cpp:82
Field.h
f2c::types::Field::isCoordSystemUTM
bool isCoordSystemUTM() const
Definition: Field.cpp:94
f2c::types::Field::coord_sys_
std::string coord_sys_
Definition: Field.h:69
f2c::types::Cells
Definition: Cells.h:21
f2c::types::Field::Field
Field()
f2c::types::Field::getUTMDatum
std::string getUTMDatum() const
Definition: Field.cpp:122
f2c::types::Point
Definition: Point.h:21
f2c::types::Field::clone
Field clone() const
Definition: Field.cpp:76
f2c::types::Field::id_
std::string id_
Definition: Field.h:68
f2c::types::Field::getCellsAbsPosition
Cells getCellsAbsPosition() const
Definition: Field.cpp:160
f2c::types::Field::getUTMZone
std::string getUTMZone() const
Definition: Field.cpp:168
f2c::types::Field::setField
void setField(const Cells &_field)
Definition: Field.cpp:72
f2c::types::Field::setId
void setId(const std::string &_id)
Definition: Field.cpp:32
f2c::types::Field::setPrevCRS
void setPrevCRS(const std::string &prev_crs)
Definition: Field.cpp:48
f2c::types::Field::getId
std::string getId() const
Definition: Field.cpp:28
f2c::types::to_string
std::string to_string(double d, const int precision=6)
Definition: Path.cpp:274
f2c::types::Field::getPrevCRS
std::string getPrevCRS() const
Definition: Field.cpp:44
f2c::types::Field::getCRS
std::string getCRS() const
Definition: Field.cpp:36


fields2cover
Author(s):
autogenerated on Fri Apr 25 2025 02:18:31