forked from ICODE-MDA/ShipDetectionApp
/
shapefileClip.cpp
368 lines (301 loc) · 9.97 KB
/
shapefileClip.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*******************************************************************************************
* File: shapefileClip.cpp
*
* Description: This code reads in an input shapefile (often the World Vector Shoreline) and crops the shapefile to fit into the image boundary.
*
*******************************************************************************************/
#include "shapefileClip.h"
shapefileClip::shapefileClip(ossimRefPtr<ossimImageHandler> handler, ossimFilename shpName)
:m_handler(handler),
m_landmaskShpName(shpName)
{
}
int shapefileClip::getClippedSHP(vector<ossimPolygon> &polys, ImageType &imageComposition)
{
string croppedShpStr = "clippedPolygons.shp";
// Determine if only shapefile exists and delete it.
ifstream polygonFile(croppedShpStr.c_str());
if (polygonFile){
std::remove(croppedShpStr.c_str());
}
char* croppedFilename = (char*)croppedShpStr.c_str();
ossimRefPtr<ossimImageGeometry> ImageGeom;
ImageGeom = m_handler->getImageGeometry();
// Set projection via geom info
ossimProjection *proj = ImageGeom->getProjection();
SHPHandle hShoreline;
OpenShapeFile(hShoreline);
vector<ossimPolygon> clippedPolys, holePolygons;
ossimDrect imageBounds;
imageBounds.setOrientMode(OSSIM_RIGHT_HANDED);
imageBounds = LineSampleToWorld(m_handler->getBoundingRect(0), ImageGeom);
// Find vertices within image rectangle boundary and save as a vector of ossimPolygons
vector<int> ShapeID;
bool allLandFlag = FindVerticesInImage(hShoreline, imageBounds, ShapeID);
if (!ShapeID.empty()){
//Image contains land and water
imageComposition = LAND_AND_WATER;
// Clips polygons to image rectange boundary
ClipShapesToRect(polys, holePolygons, ShapeID, hShoreline, imageBounds);
CreateShapeFile(polys, croppedFilename);
SHPClose(hShoreline);
}
else if (allLandFlag){
//Image contains only land
imageComposition = ALL_LAND;
SHPClose(hShoreline);
}
else{
//Image contains no land
imageComposition = ALL_WATER;
SHPClose(hShoreline);
}
return 0;
}
void shapefileClip::ClipShapesToRect(vector<ossimPolygon> &outputPolys, vector<ossimPolygon> &holePolygons, vector<int> &ShapeID, SHPHandle &hSHP, ossimDrect &imageBoundRect)
{
if (!outputPolys.empty())
outputPolys.clear();
if (ShapeID.empty())
return;
if (hSHP == NULL)
{
cerr << "Shapefile is not open." << endl;
}
SHPObject *obj;
ossimPolygon poly;
vector<ossimDpt> vertices;
vector<ossimPolygon> clippedPolys;
ossimDpt firstVertex, lastVertex;
int totalVert;
//
// Convert the shapes from SHPObject to ossimPolygon. Clip the polygons
// to the bounds of the tile.
//
for (unsigned int shapeNum = 0; shapeNum < ShapeID.size(); ++shapeNum)
{
obj = SHPReadObject(hSHP, ShapeID[shapeNum]);
if (!obj)
{
cerr << "Shape object unable to load. " << endl
<< "Line: " << __LINE__ << " " << __FILE__ << endl;
SHPClose(hSHP);
exit(EXIT_FAILURE);
}
int partStart, partEnd;
//
// Shapes may have more than one part, so we need to
// extract these polygons from the shape.
//
for (int part = 0; part < obj->nParts; ++part)
{
partStart = obj->panPartStart[part];
if (part != obj->nParts - 1)
partEnd = obj->panPartStart[part + 1];
else
partEnd = obj->nVertices;
for (int i = partStart; i < partEnd; ++i)
vertices.push_back(ossimDpt(obj->padfX[i], obj->padfY[i]));
poly = vertices;
poly.clipToRect(clippedPolys, imageBoundRect);
if (clippedPolys.empty())
continue;
for (unsigned int polyNum = 0; polyNum < clippedPolys.size(); ++polyNum){
// Check if first and last vertex match, otherwise add first vertex to polygon
totalVert = clippedPolys[polyNum].getNumberOfVertices();
clippedPolys[polyNum].vertex(0, firstVertex);
clippedPolys[polyNum].vertex(totalVert-1, lastVertex);
if (firstVertex != lastVertex){
clippedPolys[polyNum].addPoint(firstVertex);
}
if (part == 0){
outputPolys.push_back(clippedPolys[polyNum]);
}
else{
holePolygons.push_back(clippedPolys[polyNum]);
}
}
clippedPolys.clear();
vertices.clear();
}
clippedPolys.clear();
vertices.clear();
SHPDestroyObject(obj);
}
}
void shapefileClip::OpenShapeFile(SHPHandle &hSHP)
{
if ((hSHP = SHPOpen(m_landmaskShpName.c_str(),"rb")) == NULL){
cerr << "Shapefile " << m_landmaskShpName.c_str() << " could not be opened." << endl
<< "Line: " << __LINE__ << " " << __FILE__ << endl;
}
}
bool shapefileClip::FindVerticesInImage(SHPHandle hSHP, ossimDrect imgRect, vector<int> &ShapeID)
{
if (!ShapeID.empty())
ShapeID.clear();
if (hSHP == NULL){
cerr << "Shapefile is not open." << endl
<< "Line: " << __LINE__ << " " << __FILE__ << endl;
}
// Get min and max values for image bounding rectangle.
double minx, miny, maxx, maxy;
imgRect.getBounds(minx, miny, maxx, maxy);
ossimDrect newImgRect;
newImgRect = ossimDrect(ossimDpt(minx, miny), ossimDpt(maxx, maxy));
// Determine if image is completely within any of the wvs polygons
bool allLandFlag = false;
bool imgCompWithinPoly = false;
bool onePointInPoly = false;
SHPObject *obj;
ossimDrect wvsPolyRect;
int withinBoundsVertCount = 0;
// Determine number of polygons in image
unsigned long int totalPolyCount = 0;
// loop through each shape in file
while( obj = SHPReadObject(hSHP, totalPolyCount)){
totalPolyCount++;
wvsPolyRect = ossimDrect(ossimDpt(obj->dfXMin, obj->dfYMin), ossimDpt(obj->dfXMax, obj->dfYMax));
imgCompWithinPoly = false;
if (newImgRect.completely_within(wvsPolyRect)){
imgCompWithinPoly = true;
allLandFlag = true;
}
if (wvsPolyRect.intersects(newImgRect) || wvsPolyRect.completely_within(newImgRect) || imgCompWithinPoly){
withinBoundsVertCount = 0;
if (obj->nParts > 0 && obj->nVertices > 1){
// loop through vertices of qualified shape
for (unsigned long int shapeVertex = 0; shapeVertex < (unsigned)obj->nVertices && withinBoundsVertCount==0; ++shapeVertex){
// If vertex is within bounding box, then save
if ((double)obj->padfX[shapeVertex] > minx &&
(double)obj->padfX[shapeVertex] < maxx &&
(double)obj->padfY[shapeVertex] > miny &&
(double)obj->padfY[shapeVertex] < maxy){
++withinBoundsVertCount;
}
}
if (withinBoundsVertCount){
ShapeID.push_back(obj->nShapeId);
}
else if (imgCompWithinPoly){ // If bounding box completely within polygon and no vertices found
ossimPolygon entirePoly;
for (int j = 0; j < obj->nVertices; ++j)
entirePoly.addPoint(ossimDpt(obj->padfX[j], obj->padfY[j]));
// Check if image upper left point is within the shapefile polygon
if (entirePoly.isPointWithin(ossimDpt(minx, miny))){
onePointInPoly = true;
}
}
}
}
// Image completely within flag
imgCompWithinPoly = false;
SHPDestroyObject(obj);
}
SHPDestroyObject(obj);
obj = NULL;
// If image boundary is completely within a polygon, but vertices were found within the image boundary, image contains all land
if (allLandFlag && onePointInPoly && ShapeID.empty()){
return true;
}
else{
return false;
}
}
ossimDrect shapefileClip::LineSampleToWorld(ossimIrect rect, ossimRefPtr<ossimImageGeometry> ImageGeom)
{
ossimGpt gp1;
ossimGpt gp2;
ossimGpt gp3;
ossimGpt gp4;
ImageGeom->localToWorld(rect.ul(), gp1);
ImageGeom->localToWorld(rect.ur(), gp2);
ImageGeom->localToWorld(rect.lr(), gp3);
ImageGeom->localToWorld(rect.ll(), gp4);
ossimDrect boundsRect(ossimDpt(gp1.lond(), gp1.latd()),
ossimDpt(gp2.lond(), gp2.latd()),
ossimDpt(gp3.lond(), gp3.latd()),
ossimDpt(gp4.lond(), gp4.latd()),
OSSIM_RIGHT_HANDED);
return boundsRect;
}
void shapefileClip::CreateShapeFile(vector<ossimPolygon> &polys, const char *shapeFilename)
{
if (polys.empty())
{
cerr << "The polygons do not exist...cannot create shape file" << endl
<< "Line: " << __LINE__ << " " << __FILE__ << endl;
return;
}
SHPObject *psObject;
SHPHandle hSHP;
double *px, *py;
vector<ossimPolygon>::iterator polyIter;
vector<ossimDpt> vertices;
vector<ossimDpt>::iterator vertIter;
hSHP = SHPCreate(shapeFilename, SHPT_POLYGON);
if( hSHP == NULL || shapeFilename == NULL)
{
cerr << "Unable to create: " << shapeFilename << endl
<< "Line: " << __LINE__ << " " << __FILE__ << endl;
exit(EXIT_FAILURE);
}
// Build DBF.
DBFHandle hDBF;
hDBF = DBFCreate(shapeFilename);
if( hDBF == NULL)
{
cerr << "Unable to create: " << shapeFilename << " DBF File" << endl
<< "Line: " << __LINE__ << " " << __FILE__ << endl;
exit(EXIT_FAILURE);
}
// Add fields.
DBFAddField(hDBF, "poly_num", FTString, 30, 0);
polyIter = polys.begin();
int arrayIndex;
int poly_num = 0;
while (polyIter != polys.end())
{
DBFWriteStringAttribute(hDBF, poly_num, 0, ossimString::toString(poly_num));
vertices = (*polyIter).getVertexList();
vertIter = vertices.begin();
double *x = new double[vertices.size()];
double *y = new double[vertices.size()];
int *panParts = new int[vertices.size()];
panParts[0] = 0;
// SHPCreateObject deletes these
px = &x[0];
py = &y[0];
arrayIndex = 0;
while (vertIter != vertices.end())
{
x[arrayIndex] = (*vertIter).x;
y[arrayIndex++] = (*vertIter).y;
vertIter++;
}
psObject = SHPCreateObject( SHPT_POLYGON, -1, 1, panParts, NULL,
vertices.size(), px, py, NULL, NULL );
if (psObject != NULL)
{
SHPWriteObject( hSHP, -1, psObject );
SHPDestroyObject( psObject );
}
else
{
cerr << "Shape object could not be written to file" << endl
<< "Line: " << __LINE__ << " " << __FILE__ << endl;
SHPClose(hSHP);
delete []x;
delete []y;
delete [] panParts;
exit(EXIT_FAILURE);
}
polyIter++;
poly_num++;
delete []x;
delete []y;
delete [] panParts;
}
DBFClose(hDBF);
SHPClose( hSHP );
}