Skip to content

Commit bdcdbfd

Browse files
committed
Add additional checks
1 parent d884509 commit bdcdbfd

10 files changed

+174
-25
lines changed

LandRGraph.cpp

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -647,12 +647,21 @@ void LandRGraph::exportToShp(const std::string& FilePath,
647647

648648
void LandRGraph::setAttributeFromVectorId(const std::string& AttributeName,
649649
openfluid::core::GeoVectorValue& Vector,
650-
const std::string& Column)
650+
const std::string& IdColumn,
651+
const std::string& ValueColumn)
651652
{
652-
if (!Vector.containsField(Column))
653+
if (!Vector.containsField(ValueColumn))
653654
{
654655
std::ostringstream s;
655-
s << "Unable to find the column " << Column << " in GeoVector.";
656+
s << "Unable to find the column " << ValueColumn << " in GeoVector.";
657+
throw openfluid::base::FrameworkException(
658+
OPENFLUID_CODE_LOCATION, s.str());
659+
}
660+
661+
if (!Vector.containsField(IdColumn))
662+
{
663+
std::ostringstream s;
664+
s << "Unable to find the column " << IdColumn << " in GeoVector.";
656665
throw openfluid::base::FrameworkException(
657666
OPENFLUID_CODE_LOCATION, s.str());
658667
}
@@ -664,20 +673,20 @@ void LandRGraph::setAttributeFromVectorId(const std::string& AttributeName,
664673
OGRLayer* Layer0 = Vector.layer(0);
665674
Layer0->ResetReading();
666675

667-
int columnIndex=Vector.getFieldIndex(Column);
676+
int columnIndex=Vector.getFieldIndex(ValueColumn);
668677
OGRFeature* Feat;
669678
while ((Feat = Layer0->GetNextFeature()) != nullptr)
670679
{
671-
int OfldId=Feat->GetFieldAsInteger("OFLD_ID");
680+
int OfldId=Feat->GetFieldAsInteger(IdColumn.c_str());
672681
openfluid::landr::LandREntity* Entity=entity(OfldId);
673682
if (Entity)
674683
{
675-
if (Vector.isFieldOfType(Column, OFTInteger))
684+
if (Vector.isFieldOfType(ValueColumn, OFTInteger))
676685
{
677686
int value=Feat->GetFieldAsInteger(columnIndex);
678687
Entity->setAttributeValue(AttributeName, new openfluid::core::IntegerValue(value));
679688
}
680-
else if (Vector.isFieldOfType(Column, OFTReal))
689+
else if (Vector.isFieldOfType(ValueColumn, OFTReal))
681690
{
682691
double value=Feat->GetFieldAsDouble(columnIndex);
683692
Entity->setAttributeValue(AttributeName, new openfluid::core::DoubleValue(value));
@@ -703,12 +712,21 @@ void LandRGraph::setAttributeFromVectorId(const std::string& AttributeName,
703712

704713
void LandRGraph::setAttributeFromVectorId(const std::string& AttributeName,
705714
openfluid::landr::VectorDataset& Vector,
706-
const std::string& Column)
715+
const std::string& IdColumn,
716+
const std::string& ValueColumn)
707717
{
708-
if (!Vector.containsField(Column))
718+
if (!Vector.containsField(ValueColumn))
719+
{
720+
std::ostringstream s;
721+
s << "Unable to find the column " << ValueColumn << " in VectorDataset.";
722+
throw openfluid::base::FrameworkException(
723+
OPENFLUID_CODE_LOCATION, s.str());
724+
}
725+
726+
if (!Vector.containsField(IdColumn))
709727
{
710728
std::ostringstream s;
711-
s << "Unable to find the column " << Column << " in VectorDataset.";
729+
s << "Unable to find the column " << IdColumn << " in VectorDataset.";
712730
throw openfluid::base::FrameworkException(
713731
OPENFLUID_CODE_LOCATION, s.str());
714732
}
@@ -720,20 +738,20 @@ void LandRGraph::setAttributeFromVectorId(const std::string& AttributeName,
720738
OGRLayer* Layer0 = Vector.layer(0);
721739
Layer0->ResetReading();
722740

723-
int columnIndex=Vector.getFieldIndex(Column);
741+
int columnIndex=Vector.getFieldIndex(ValueColumn);
724742
OGRFeature* Feat;
725743
while ((Feat = Layer0->GetNextFeature()) != nullptr)
726744
{
727-
int OfldId=Feat->GetFieldAsInteger("OFLD_ID");
745+
int OfldId=Feat->GetFieldAsInteger(IdColumn.c_str());
728746
openfluid::landr::LandREntity* Entity=entity(OfldId);
729747
if (Entity)
730748
{
731-
if (Vector.isFieldOfType(Column, OFTInteger))
749+
if (Vector.isFieldOfType(ValueColumn, OFTInteger))
732750
{
733751
int value=Feat->GetFieldAsInteger(columnIndex);
734752
Entity->setAttributeValue(AttributeName, new openfluid::core::IntegerValue(value));
735753
}
736-
else if (Vector.isFieldOfType(Column, OFTReal))
754+
else if (Vector.isFieldOfType(ValueColumn, OFTReal))
737755
{
738756
double value=Feat->GetFieldAsDouble(columnIndex);
739757
Entity->setAttributeValue(AttributeName, new openfluid::core::DoubleValue(value));

LandRGraph.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,25 +295,29 @@ class OPENFLUID_API LandRGraph: public geos::planargraph::PlanarGraph
295295

296296
/**
297297
@brief Creates a new attribute for all the LandREntity of this LandRGraph, and set for each LandREntity
298-
this attribute value as the vector value corresponding to the entity OFLD_ID.
298+
this attribute value as the vector value corresponding to the entity ID number.
299299
@param AttributeName The name of the attribute to create.
300300
@param Vector The Name of the core::GeoVectorValue.
301-
@param Column The column of the core::GeoVectorValue to upload.
301+
@param IdColumn The ID number column of the core::GeoVectorValue.
302+
@param ValueColumn The column of the core::GeoVectorValue to upload.
302303
*/
303304
void setAttributeFromVectorId(const std::string& AttributeName,
304305
openfluid::core::GeoVectorValue& Vector,
305-
const std::string& Column);
306+
const std::string& IdColumn,
307+
const std::string& ValueColumn);
306308

307309
/**
308310
@brief Creates a new attribute for all the LandREntity of this LandRGraph, and set for each LandREntity
309311
this attribute value as the vector value corresponding to the entity OFLD_ID.
310312
@param AttributeName The name of the attribute to create.
311313
@param Vector The Name of the VectorDataset.
312-
@param Column The column of the core::GeoVectorValue to upload.
314+
@param IdColumn The ID number column of the landr::VectorDataset.
315+
@param ValueColumn The column of the landr::VectorDataset to upload.
313316
*/
314317
void setAttributeFromVectorId(const std::string& AttributeName,
315318
openfluid::landr::VectorDataset& Vector,
316-
const std::string& Column);
319+
const std::string& IdColumn,
320+
const std::string& ValueColumn);
317321

318322
/**
319323
@brief Creates a new attribute for all the LandREntity of this LandRGraph, and set for each LandREntity

LineStringGraph.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ LineStringGraph::~LineStringGraph()
111111

112112
LineStringGraph* LineStringGraph::create(openfluid::core::GeoVectorValue& Val)
113113
{
114+
if (!Val.isLineType())
115+
throw openfluid::base::FrameworkException(
116+
OPENFLUID_CODE_LOCATION,
117+
"GeoVectorValue is not Line type");
118+
114119
LineStringGraph* Graph = new LineStringGraph(Val);
115120
Graph->addEntitiesFromGeoVector();
116121

@@ -124,6 +129,12 @@ LineStringGraph* LineStringGraph::create(openfluid::core::GeoVectorValue& Val)
124129

125130
LineStringGraph* LineStringGraph::create(openfluid::landr::VectorDataset& Vect)
126131
{
132+
133+
if (!Vect.isLineType())
134+
throw openfluid::base::FrameworkException(
135+
OPENFLUID_CODE_LOCATION,
136+
"VectorDataset is not Line type");
137+
127138
LineStringGraph* Graph = new LineStringGraph(Vect);
128139
Graph->addEntitiesFromGeoVector();
129140

PolygonEntity.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ void PolygonEntity::addEdge(PolygonEdge& Edge)
132132
m_PolyEdges.push_back(&Edge);
133133

134134
mp_NeighboursMap = 0;
135+
136+
mp_LineStringNeighboursMap = 0;
135137
}
136138

137139

@@ -154,6 +156,8 @@ void PolygonEntity::removeEdge(PolygonEdge* Edge)
154156

155157
mp_NeighboursMap = 0;
156158

159+
mp_LineStringNeighboursMap = 0;
160+
157161
delete Edge;
158162
}
159163

PolygonGraph.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ PolygonGraph::PolygonGraph(openfluid::landr::VectorDataset& Vect) :
100100

101101
PolygonGraph* PolygonGraph::create(openfluid::core::GeoVectorValue& Val)
102102
{
103+
if (!Val.isPolygonType())
104+
throw openfluid::base::FrameworkException(
105+
OPENFLUID_CODE_LOCATION,
106+
"GeoVectorValue is not Polygon type");
107+
103108
PolygonGraph* Graph = new PolygonGraph(Val);
104109
try{
105110

@@ -122,6 +127,11 @@ PolygonGraph* PolygonGraph::create(openfluid::core::GeoVectorValue& Val)
122127

123128
PolygonGraph* PolygonGraph::create(openfluid::landr::VectorDataset& Vect)
124129
{
130+
if (!Vect.isPolygonType())
131+
throw openfluid::base::FrameworkException(
132+
OPENFLUID_CODE_LOCATION,
133+
"VectorDataset is not Polygon type");
134+
125135
PolygonGraph* Graph = new PolygonGraph(Vect);
126136
try{
127137

VectorDataset.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,11 @@ std::string VectorDataset::checkTopology(double Threshold, unsigned int LayerInd
950950

951951
std::list<std::pair<OGRFeature*, OGRFeature*> > VectorDataset::findOverlap(unsigned int LayerIndex)
952952
{
953+
if ( ! isPolygonType(LayerIndex))
954+
throw openfluid::base::FrameworkException(
955+
OPENFLUID_CODE_LOCATION,
956+
"the VectorDataset is not Polygon type.");
957+
953958
m_Features.clear();
954959
m_Geometries.clear();
955960
std::list<std::pair<OGRFeature*,OGRFeature*> > lOverlaps;
@@ -991,6 +996,11 @@ std::list<std::pair<OGRFeature*, OGRFeature*> > VectorDataset::findOverlap(unsig
991996

992997
std::list<std::pair<OGRFeature*,OGRFeature*> > VectorDataset::findGap(double Threshold, unsigned int LayerIndex)
993998
{
999+
if ( ! isPolygonType(LayerIndex))
1000+
throw openfluid::base::FrameworkException(
1001+
OPENFLUID_CODE_LOCATION,
1002+
"the VectorDataset is not Polygon type.");
1003+
9941004
m_Features.clear();
9951005
m_Geometries.clear();
9961006
std::list<std::pair<OGRFeature*,OGRFeature*> > lGaps;
@@ -1033,6 +1043,10 @@ std::list<std::pair<OGRFeature*,OGRFeature*> > VectorDataset::findGap(double Thr
10331043

10341044
void VectorDataset::cleanOverlap(double Threshold, unsigned int LayerIndex)
10351045
{
1046+
if ( ! isPolygonType(LayerIndex))
1047+
throw openfluid::base::FrameworkException(
1048+
OPENFLUID_CODE_LOCATION,
1049+
"the VectorDataset is not Polygon type.");
10361050

10371051
m_Features.clear();
10381052
m_Geometries.clear();

VectorDataset.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,15 @@ class OPENFLUID_API VectorDataset
295295

296296
/**
297297
@brief Snap the vertices of this VectorDataset.
298+
Only for Polygon or Line Type;
298299
@param Threshold The snapping threshold value.
299300
@param LayerIndex The index of the layer to query, default 0.
300301
*/
301302
void snapVertices(double Threshold,unsigned int LayerIndex=0);
302303

303304
/**
304305
@brief Check the topology of this VectorDataset.
306+
Only for Polygon Type.
305307
@param Threshold The maximum distance between polygon to be considered as gap.
306308
@param LayerIndex The index of the layer to query, default 0.
307309
@return An non-empty string if found errors, otherwise the string is empty.
@@ -310,13 +312,15 @@ class OPENFLUID_API VectorDataset
310312

311313
/**
312314
@brief Find the overlapping polygons.
315+
Only for Polygon Type;
313316
@param LayerIndex The index of the layer to query, default 0.
314317
@return A list of pair of OGRFeature* for each overlap between two polygons.
315318
*/
316319
std::list<std::pair<OGRFeature*, OGRFeature*> > findOverlap(unsigned int LayerIndex=0);
317320

318321
/**
319322
@brief Find gap between polygons.
323+
Only for Polygon Type;
320324
@param Threshold The maximum distance between polygon to be considered as gap.
321325
@param LayerIndex The index of the layer to query, default 0.
322326
@return A list of pair of OGRFeature* for each overlap between two polygons.
@@ -325,6 +329,7 @@ class OPENFLUID_API VectorDataset
325329

326330
/**
327331
@brief Clean the overlapping polygons.
332+
Only for Polygon Type;
328333
@param Threshold The snapping threshold value.
329334
@param LayerIndex The index of the layer to query, default 0.
330335
*/

tests/LandRGraph_TEST.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,10 @@ BOOST_AUTO_TEST_CASE(check_get_AVectorAttribute_from_Id_for_LineStringGraph)
266266
openfluid::landr::LineStringGraph* Graph =
267267
openfluid::landr::LineStringGraph::create(*Vector);
268268

269-
BOOST_CHECK_THROW(Graph->setAttributeFromVectorId("attribut",*Vector, "No_col"),openfluid::base::FrameworkException);
269+
BOOST_CHECK_THROW(Graph->setAttributeFromVectorId("attribut",*Vector, "OFLD_ID","No_col"),openfluid::base::FrameworkException);
270+
BOOST_CHECK_THROW(Graph->setAttributeFromVectorId("attribut",*Vector, "No_col","USR_LEN"),openfluid::base::FrameworkException);
270271

271-
Graph->setAttributeFromVectorId("attribut",*Vector, "USR_LEN");
272+
Graph->setAttributeFromVectorId("attribut",*Vector, "OFLD_ID","USR_LEN");
272273
std::vector<std::string> vAttributes=Graph->getAttributeNames();
273274
BOOST_CHECK_EQUAL(vAttributes.empty(),false);
274275

@@ -289,7 +290,7 @@ BOOST_AUTO_TEST_CASE(check_get_AVectorAttribute_from_Id_for_LineStringGraph)
289290
openfluid::core::GeoVectorValue* OtherVector = new openfluid::core::GeoVectorValue(
290291
CONFIGTESTS_INPUT_MISCDATA_DIR + "/landr", "badRS_misdirected.shp");
291292

292-
Graph->setAttributeFromVectorId("attribut",*OtherVector, "USR_SLOP");
293+
Graph->setAttributeFromVectorId("attribut",*OtherVector, "OFLD_ID","USR_SLOP");
293294

294295
Entity=Graph->entity(1);
295296
Entity->getAttributeValue("attribut", DoubleValue);
@@ -306,7 +307,7 @@ BOOST_AUTO_TEST_CASE(check_get_AVectorAttribute_from_Id_for_LineStringGraph)
306307
Value);
307308

308309

309-
Graph->setAttributeFromVectorId("attribut",*Vect, "USR_WID");
310+
Graph->setAttributeFromVectorId("attribut",*Vect, "OFLD_ID","USR_WID");
310311

311312
Entity=Graph->entity(1);
312313
Entity->getAttributeValue("attribut", DoubleValue);
@@ -338,9 +339,10 @@ BOOST_AUTO_TEST_CASE(check_get_AVectorAttribute_from_Id_for_PolygonGraph)
338339
openfluid::landr::PolygonGraph* Graph =
339340
openfluid::landr::PolygonGraph::create(*Vector);
340341

341-
BOOST_CHECK_THROW(Graph->setAttributeFromVectorId("attribut",*Vector, "No_col"),openfluid::base::FrameworkException);
342+
BOOST_CHECK_THROW(Graph->setAttributeFromVectorId("attribut",*Vector,"OFLD_ID", "No_col"),openfluid::base::FrameworkException);
343+
BOOST_CHECK_THROW(Graph->setAttributeFromVectorId("attribut",*Vector,"No_col", "FLOW_CDE"),openfluid::base::FrameworkException);
342344

343-
Graph->setAttributeFromVectorId("attribut",*Vector, "FLOW_CDE");
345+
Graph->setAttributeFromVectorId("attribut",*Vector,"OFLD_ID", "FLOW_CDE");
344346
std::vector<std::string> vAttributes=Graph->getAttributeNames();
345347
BOOST_CHECK_EQUAL(vAttributes.empty(),false);
346348

tests/LineStringGraph_TEST.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,3 +1095,45 @@ BOOST_AUTO_TEST_CASE(check_mergeLineStringEntitiesByMinLength)
10951095

10961096
// =====================================================================
10971097
// =====================================================================
1098+
1099+
1100+
BOOST_AUTO_TEST_CASE(check_construction_from_non_LineType)
1101+
{
1102+
openfluid::core::GeoVectorValue* Val = new openfluid::core::GeoVectorValue(
1103+
CONFIGTESTS_INPUT_MISCDATA_DIR + "/landr", "SU.shp");
1104+
1105+
openfluid::landr::LineStringGraph* Graph1 = NULL;
1106+
BOOST_CHECK_THROW(Graph1 = openfluid::landr::LineStringGraph::create(*Val),openfluid::base::FrameworkException);
1107+
1108+
delete Val;
1109+
delete Graph1;
1110+
1111+
1112+
openfluid::core::GeoVectorValue Val2(CONFIGTESTS_INPUT_MISCDATA_DIR + "/landr",
1113+
"SU.shp");
1114+
1115+
openfluid::landr::VectorDataset* Vect =
1116+
new openfluid::landr::VectorDataset(Val2);
1117+
1118+
openfluid::landr::LineStringGraph* Graph2=NULL;
1119+
BOOST_CHECK_THROW( Graph2 = openfluid::landr::LineStringGraph::create(*Vect),
1120+
openfluid::base::FrameworkException);
1121+
1122+
delete Vect;
1123+
delete Graph2;
1124+
1125+
openfluid::core::GeoVectorValue* Val3 = new openfluid::core::GeoVectorValue(
1126+
CONFIGTESTS_INPUT_MISCDATA_DIR + "/landr", "PU.shp");
1127+
1128+
openfluid::landr::LineStringGraph* Graph3 = NULL;
1129+
BOOST_CHECK_THROW(Graph3 = openfluid::landr::LineStringGraph::create(*Val3),openfluid::base::FrameworkException);
1130+
1131+
delete Val3;
1132+
delete Graph3;
1133+
1134+
}
1135+
1136+
1137+
// =====================================================================
1138+
// =====================================================================
1139+

0 commit comments

Comments
 (0)