6 #include <geos/geom/Coordinate.h> 7 #include <geos/geom/LineSegment.h> 8 #include <geos/geosAlgorithm.h> 9 #include <geos/simplify/DouglasPeuckerLineSimplifier.h> 10 #include <geos/algorithm/distance/DiscreteHausdorffDistance.h> 12 #include <terralib/geometry/Coord2d.h> 20 for (
size_t i = 0; i < pts.size(); i++)
35 if ((a.
x -
c.
x) >= 0 && (b.
x -
c.
x) < 0)
37 if ((a.
x -
c.
x) < 0 && (b.
x -
c.
x) >= 0)
39 if ((a.
x -
c.
x) == 0 && (b.
x -
c.
x) == 0) {
40 if (a.
y -
c.
y >= 0 || b.
y -
c.
y >= 0)
46 double det = (a.
x -
c.
x) * (b.
y -
c.
y) - (b.
x -
c.
x) * (a.
y -
c.
y);
54 double d1 = (a.
x -
c.
x) * (a.
x -
c.
x) + (a.
y -
c.
y) * (a.
y -
c.
y);
55 double d2 = (b.
x -
c.
x) * (b.
x -
c.
x) + (b.
y -
c.
y) * (b.
y -
c.
y);
63 double Distance(
const double pt1x,
const double pt1y,
const double pt2x,
const double pt2y)
86 float dx = (float)fx - (
float)lx;
87 float coef = std::numeric_limits< float >::max();
89 coef = ((float)fy - (
float)ly) / dx;
99 if (dcc > d0c && dcc > d1c)
105 if (m01 == std::numeric_limits< float >::max())
120 if (m0c == std::numeric_limits< float >::max())
148 px, py, lx, ly, lpx, lpy, s;
157 a = py * lx - px * ly;
158 b = lpx * ly - lpy * lx;
159 c = lpx * py - lpy * px;
167 if ((b < 0) || (b > a) || (c < 0) || (c > a))
172 if ((b > 0) || (b < a) || (c > 0) || (c < a))
178 pt->
x = (pfr.
getX() + (px*s));
179 pt->
y = (pfr.
getY() + (py*s));
185 double SegmentDistance(
double fx,
double fy,
double lx,
double ly,
double ptx,
double pty,
double *pix,
double *piy)
189 double vx = ptx - fx;
190 double vy = pty - fy;
191 double uv = (ux*vx + uy*vy);
199 return (sqrt(vx*vx + vy*vy));
207 uv = (ux*vx + uy*vy);
215 return (sqrt(vx*vx + vy*vy));
219 double a,
b,
c, k, dist, aabb;
226 dist = fabs((a*ptx + b*pty + c) / (sqrt(aabb)));
230 *pix = (b*k - a*
c) / aabb;
231 *piy = (-a*k - b*
c) / aabb;
248 geos::geom::LineSegment ls(fseg.
x, fseg.
y, lseg.
x, lseg.
y);
249 geos::geom::Coordinate
c(pt.
x, pt.
y);
250 geos::geom::Coordinate c_int;
253 ls.closestPoint(c, c_int);
257 return(ls.distance(c));
263 std::vector<geos::geom::Coordinate> coordpts;
264 for (std::size_t j = 0; j < ls->
size(); ++j)
266 std::unique_ptr<te::gm::Point> lpt = std::move(ls->
getPointN(j));
267 geos::geom::Coordinate coo(lpt->getX(), lpt->getY());
268 coordpts.push_back(coo);
270 geos::simplify::DouglasPeuckerLineSimplifier douglas(coordpts);
271 douglas.setDistanceTolerance(snap);
272 geos::simplify::DouglasPeuckerLineSimplifier::CoordsVectAutoPtr simplified = douglas.simplify();
273 if (simplified.get()->at(0) != simplified.get()->at(simplified->size() - 1))
274 simplified->insert(simplified->end(), simplified.get()->at(0));
278 for (std::size_t j = 0; j < simplified->size(); ++j)
280 geos::geom::Coordinate c1 = simplified.get()->at(j);
309 double d0 =
Distance(m_seg.getCoordinates()[0], m_centroid);
310 double d1 =
Distance(m_seg.getCoordinates()[1], m_centroid);
311 double dp =
Distance(pt, m_centroid);
312 if (dp < d0 && dp < d1)
316 float dx = (float)m_seg.getCoordinates()[1].getX() - (float)m_seg.getCoordinates()[0].getX();
317 float dxpt = (float)pt.
getX() - (float)m_seg.getCoordinates()[0].getX();
318 float coef_seg = std::numeric_limits< float >::max();
319 float coef_pt = std::numeric_limits< float >::max();
321 coef_seg = ((float)m_seg.getCoordinates()[1].getY() - (float)m_seg.getCoordinates()[0].getY()) / dx;
323 coef_pt = ((float)pt.
getY() - (float)m_seg.getCoordinates()[0].getY()) / dxpt;
341 if (coef_pt < coef_seg)
349 m_inter.push_back(inter);
354 std::unique_ptr<te::da::DataSourceTransactor> t = source->
getTransactor();
356 std::map<std::string, std::string> options;
360 if (source->
getType() ==
"OGR")
367 std::string name = outDsType->
getName();
368 source->
add(name, result, options);
375 t->createDataSet(outDsType, options);
379 std::string name = outDsType->
getName();
380 t->add(name, result, options);
391 catch (std::exception& e)
401 for (
size_t nlr = 0; nlr < pol->
getNumRings(); nlr++)
404 for (
size_t i = 0; i < lr->
getNPoints() - 1; i++)
407 m_nodetree->insert(pt, std::pair<int32_t, int32_t>(
id, (int32_t)i));
416 std::vector<te::gm::Line*> reportline;
418 for (
size_t nlr = 0; nlr < pol->
getNumRings(); nlr++)
421 for (
size_t i = 0; i < lr->
getNPoints() - 1; i++)
435 for (
size_t nlr = 0; nlr < pol->
getNumRings(); nlr++)
438 for (
size_t i = 0; i < lr->
getNPoints() - 1; i++)
451 std::map<std::string, std::string> srcInfo;
452 srcInfo[
"URI"] = filename;
453 srcInfo[
"DRIVER"] =
"ESRI Shapefile";
458 if (!srcDs->dataSetExists(inDsetName))
460 std::cout <<
"Input dataset not found: " << inDsetName << std::endl;
464 std::unique_ptr<te::da::DataSet> inDset = srcDs->getDataSet(inDsetName);
465 std::unique_ptr<te::da::DataSetType> inDsetType = srcDs->getDataSetType(inDsetName);
469 inDset->moveBeforeFirst();
470 while (inDset->moveNext())
472 std::unique_ptr<te::gm::Geometry> gin = inDset->getGeometry(geo_pos);
473 std::string geostype = gin.get()->getGeometryType();
474 if (geostype ==
"MultiPolygon")
478 for (std::size_t i = 0; i < np; ++i)
485 m_pol.insert(std::pair<size_t, te::gm::Polygon*>(inDset->getInt32(id_pos),
p));
487 int32_t
id = (int32_t)inDset->getInt32(id_pos);
489 m_original_area[i] = area;
492 Polygon2RTree(p, m_rtree);
497 Polygon2Points(p, inDset->getInt32(id_pos));
502 if (geostype ==
"Polygon")
508 m_pol.insert(std::pair<size_t, te::gm::Polygon*>(inDset->getInt32(id_pos),
p));
510 Polygon2RTree(p, m_rtree);
515 Polygon2Points(p, inDset->getInt32(id_pos));
528 m_tol = m_dist_min / 5;
529 m_filename1 =
"D:/Dados_GAP/AGD2015_22761_pol.shp";
530 m_inDS1 =
"AGD2015_22761_pol";
531 m_filename2 =
"D:/Dados_GAP/desflorestamento4674_dissolve.shp";
532 m_inDS2 =
"desflorestamento4674_dissolve";
542 m_nodetree_step2 =
new KD_TREE(e);
554 if (!LoadPolygons(m_filename1, m_inDS1,
RTree_out))
557 if (!LoadPolygons(m_filename1, m_inDS1,
Point_out))
560 if (!LoadPolygons(m_filename2, m_inDS2,
Polygon_out))
563 std::vector< std::pair< size_t, te::gm::Polygon* > > pols_step00;
564 std::vector< std::pair< size_t, te::gm::Polygon* > > pols_step0;
565 std::vector< std::pair< size_t, te::gm::Polygon* > > pols_step1;
566 std::vector< std::pair< size_t, te::gm::Polygon* > > pols_step2;
567 std::vector< std::pair< size_t, te::gm::Polygon* > > pols_step3;
568 std::vector< std::pair< size_t, te::gm::Polygon* > > pols_final;
571 if (step0(pols_step00, pols_step0))
573 if (step1(pols_step0, pols_step1))
575 if (step2(pols_step1, pols_step2))
579 if (step3(pols_step2, pols_step3))
582 SavePol(pols_step3,
"step3");
583 if (step4(pols_step3, pols_final))
586 SavePol(pols_final,
"final");
597 bool GAP::step00(std::vector< std::pair< size_t, te::gm::Polygon* > > &polsout)
599 for (std::map<size_t, te::gm::Polygon*>::iterator it = m_pol.begin(); it != m_pol.end(); it++)
602 int32_t polid = (int32_t)it->first;
605 if (area < (m_dist_min*m_dist_min) || !cent)
607 std::cout <<
"Polygon ERROR 0" << polid <<
" area " << area << std::endl;
625 m_pol_points.push_back(
GAP_inter(polid, (int32_t)i, p_d.getX(), p_d.getY(), p_d.getX(), p_d.getY(), p_d.getX(), p_d.getY(),
626 0., 0.,
"desf",
true));
634 polsout.push_back(std::pair<int32_t, te::gm::Polygon *>(polid, pol_s));
640 std::cout <<
"ERROR 00 " << polid << std::endl;
646 SavePol(polsout,
"step00");
648 m_pol_points.clear();
653 bool GAP::step0(std::vector< std::pair< size_t, te::gm::Polygon* > > &pols, std::vector< std::pair< size_t, te::gm::Polygon* > > &polsout)
655 std::vector<KD_NODE*> reportsnode;
656 std::map<double, te::gm::Coord2D> p_agreg;
659 std::map< const te::gm::Coord2D, std::pair<size_t, double> > p_agreg_sel;
660 std::map< const te::gm::Coord2D, std::pair<size_t, double> >::iterator it_agreg;
661 std::vector<te::gm::Coord2D> pol_points;
663 for (std::vector< std::pair<size_t, te::gm::Polygon*> >::iterator it = pols.begin(); it != pols.end(); it++)
666 int32_t polid = (int32_t)it->first;
669 if (area < (m_dist_min*m_dist_min) || !cent)
671 std::cout <<
"Polygon ERROR " << polid <<
" area " << area << std::endl;
678 Polygon2RTree(pol, rtree_pol);
681 for (
size_t i = 0; i < lin->
getNPoints()-1; i++)
686 p_d.x + m_dist_min, p_d.y + m_dist_min);
687 m_nodetree->search(ept, reportsnode);
689 for (
size_t j = 0; j < reportsnode.size(); j++)
693 if (dist < m_dist_min)
695 if (pointLocate(c, pol) != geos::geom::Location::INTERIOR)
697 p_agreg.insert(std::pair<double, te::gm::Coord2D>(dist, c));
704 const te::gm::Coord2D c(p_agreg.begin()->second.x, p_agreg.begin()->second.y);
705 double d =
Distance(p_agreg.begin()->second, p_d);
707 for (it_agreg = p_agreg_sel.begin(); it_agreg != p_agreg_sel.end(); it_agreg++)
709 if ((*it_agreg).first ==
c)
713 if (it_agreg != p_agreg_sel.end())
715 if ((*it_agreg).second.first == 0 && i == lin->
getNPoints() - 2)
718 p_agreg_sel.insert(std::pair<
te::gm::Coord2D, std::pair<size_t, double> >(p_d, std::pair<size_t, double>(i, 0)));
721 if (d < (*it_agreg).second.second)
723 (*it_agreg).second.first = i;
724 (*it_agreg).second.second =
d;
728 p_agreg_sel.insert(std::pair<
te::gm::Coord2D, std::pair<size_t, double> >(
c, std::pair<size_t, double>(i, d)));
734 for (
size_t i = 0; i < lin->
getNPoints()-1; i++)
738 for (it_agreg = p_agreg_sel.begin(); it_agreg != p_agreg_sel.end(); it_agreg++)
740 if ((*it_agreg).second.first == i)
743 if (it_agreg != p_agreg_sel.end())
745 if ((*it_agreg).second.second == 0)
747 m_pol_points.push_back(
GAP_inter(polid, (int32_t)i, p_d.getX(), p_d.getY(), p_d.getX(), p_d.getY(), p_d.getX(), p_d.getY(),
748 0., 0.,
"not_used",
true));
753 if (!verifyIntersections(p_d, pp, rtree_pol))
755 pol_points.push_back(pp);
756 m_pol_points.push_back(
GAP_inter(polid, (int32_t)i, p_d.getX(), p_d.getY(), p_d.getX(), p_d.getY(), pp.
x, pp.
y,
757 it_agreg->second.second, it_agreg->second.second,
"agreg",
true));
761 pol_points.push_back(p_d);
762 m_pol_points.push_back(
GAP_inter(polid, (int32_t)i, p_d.getX(), p_d.getY(), p_d.getX(), p_d.getY(), p_d.getX(), p_d.getY(),
763 0., 0.,
"desf",
true));
768 pol_points.push_back(p_d);
769 m_pol_points.push_back(
GAP_inter(polid, (int32_t)i, p_d.getX(), p_d.getY(), p_d.getX(), p_d.getY(), p_d.getX(), p_d.getY(),
770 0., 0.,
"desf",
true));
776 if (pol_points.size() < 3)
778 std::cout <<
"Step0 - out Polygon ERROR " << polid << std::endl;
786 for (pp = 0; pp < pol_points.size(); pp++)
796 std::cout <<
"Step0 - ERROR " << polid << std::endl;
800 polsout.
push_back(std::pair<int32_t, te::gm::Polygon *>(polid, pol_new));
804 SavePol(polsout,
"step0");
806 m_pol_points.clear();
811 bool GAP::step1(std::vector< std::pair< size_t, te::gm::Polygon* > > &pols, std::vector< std::pair< size_t, te::gm::Polygon* > > &polsout)
813 std::vector<te::gm::Line*> reportline;
814 std::vector<te::gm::Coord2D> pol_points;
815 std::map< double, std::pair<size_t, te::gm::Coord2D> > map_seg;
816 std::map< double, std::pair<size_t, te::gm::Coord2D> > :: iterator it_seg;
817 std::map<const te::gm::Coord2D, std::pair<size_t, double> > map_pts_used;
818 std::map<const te::gm::Coord2D, std::pair<size_t, double> >::iterator it_pts_used;
820 for (std::vector< std::pair<size_t, te::gm::Polygon*> >::iterator it = pols.begin(); it != pols.end(); it++)
823 int32_t polid = (int32_t)it->first;
827 Polygon2RTree(pol, rtree_pol);
829 for (
size_t i = 0; i < lin->getNPoints()-1; i++)
834 pt0.
x + m_dist_min, pt0.
y + m_dist_min);
835 m_rtree.search(ept, reportline);
838 if (!reportline.size())
852 for (
size_t rl = 0; rl < reportline.size(); rl++)
854 dist =
coordToSegmentDistance(reportline[rl]->getCoordinates()[0], reportline[rl]->getCoordinates()[1], pt0, &ptint);
855 map_seg.insert(std::pair<
double, std::pair<size_t, te::gm::Coord2D> >(dist, std::pair<size_t, te::gm::Coord2D>(rl, ptint)));
858 for (it_seg = map_seg.begin(); it_seg != map_seg.end(); it_seg++)
860 dist = it_seg->first;
861 seg_sel = it_seg->second.first;
862 ptint = it_seg->second.second;
866 if (dist <= m_dist_min)
868 if (pointLocate(ptint, pol) != geos::geom::Location::INTERIOR)
870 if (!verifyIntersections(pt0, ptint, rtree_pol))
872 for (it_pts_used = map_pts_used.begin(); it_pts_used != map_pts_used.end(); it_pts_used++)
874 if ((*it_pts_used).first == ptint)
877 if (it_pts_used != map_pts_used.end())
879 if (dist < it_pts_used->second.second)
880 it_pts_used->second.first = i;
886 map_pts_used.insert(std::pair<
te::gm::Coord2D, std::pair<size_t, double>>(ptint, std::pair<size_t, double>(i, dist)));
912 for (
size_t i = 0; i < lin->getNPoints()-1; i++)
916 for (it_pts_used = map_pts_used.begin(); it_pts_used != map_pts_used.end(); it_pts_used++)
918 if ((*it_pts_used).second.first == i)
920 pol_points.push_back(it_pts_used->first);
921 m_pol_points.push_back(
GAP_inter(polid, (int32_t)i, pt0.
getX(), pt0.
getY(), pt0.
getX(), pt0.
getY(), it_pts_used->first.getX(), it_pts_used->first.getY(),
922 (*it_pts_used).second.second, 0.,
"agreg",
true));
929 pol_points.push_back(pt0);
931 0., 0.,
"desf",
true));
934 map_pts_used.clear();
937 if (pol_points.size() < 3)
939 std::cout <<
"Step1 - out Polygon ERROR " << polid << std::endl;
947 for (pp = 0; pp < pol_points.size(); pp++)
957 std::cout <<
"Step1 - ERROR " << polid << std::endl;
961 polsout.
push_back(std::pair<int32_t, te::gm::Polygon *>(polid, pol_new));
964 SavePol(polsout,
"step1");
966 m_pol_points.clear();
972 bool GAP::step2(std::vector< std::pair< size_t, te::gm::Polygon* > > &pols, std::vector< std::pair< size_t, te::gm::Polygon* > > &polsout)
974 std::vector<te::gm::Coord2D> pt_out;
975 std::vector<te::gm::Line*> reportline;
976 std::map<double, te::gm::Coord2D> map_int;
978 for (std::vector< std::pair<size_t, te::gm::Polygon*> >::iterator it = pols.begin(); it != pols.end(); it++)
981 int32_t polid = (int32_t)it->first;
985 for (i = 0; i < lin->getNPoints() - 1; i++)
992 m_pol_points.push_back(
GAP_inter(polid, (int32_t)i, first.getX(), first.getY(), first.getX(), first.getY(), first.getX(), first.getY(),
993 0., 0.,
"desf",
true));
997 line->setCoord(0, first.getX(), first.getY());
998 line->setCoord(1, last.getX(), last.getY());
999 double dist01 =
Distance(first, last);
1000 te::gm::Envelope ept(line->getMBR()->getLowerLeftX() - m_dist_min, line->getMBR()->getLowerLeftY() - m_dist_min,
1001 line->getMBR()->getUpperRightX() + m_dist_min, line->getMBR()->getUpperRightY() + m_dist_min);
1002 m_rtree.search(ept, reportline);
1003 if (!reportline.size())
1007 for (
size_t rl = 0; rl < reportline.size(); rl++)
1009 if (
segInterPoint(reportline[rl]->getCoordinates()[0], reportline[rl]->getCoordinates()[1], first, last, &ptint))
1011 double dist =
Distance(first, ptint);
1012 map_int.insert(std::pair<double, te::gm::Coord2D>(dist,
te::gm::Coord2D(ptint)));
1015 for (std::map<double, te::gm::Coord2D>::iterator it = map_int.begin(); it != map_int.end(); it++)
1019 m_pol_points.push_back(
GAP_inter(polid, (int32_t)i, (*it).second.getX(), (*it).second.getY(), (*it).second.getX(), (*it).second.getY(), (*it).second.getX(), (*it).second.getY(),
1020 (*it).first, 0.,
"agreg",
true));
1028 if (pt_out.size() < 3)
1030 std::cout <<
"Step2 - out Polygon ERROR " << polid << std::endl;
1038 for (pp = 0; pp < pt_out.size(); pp++)
1041 m_nodetree_step2->insert(
te::gm::Coord2D(pt_out[pp].getX(), pt_out[pp].getY()), std::pair<int32_t, int32_t>(polid, (int32_t)pp));
1049 polsout.push_back(std::pair<int32_t, te::gm::Polygon *>(polid, pol_new));
1052 SavePol(polsout,
"step2");
1054 m_pol_points.clear();
1059 bool GAP::step3(std::vector< std::pair< size_t, te::gm::Polygon* > > &pols, std::vector< std::pair< size_t, te::gm::Polygon* > > &polsout)
1061 std::vector<KD_NODE*> reportsnode;
1062 std::vector<te::gm::Coord2D> pt_out;
1063 std::map< double, std::pair<te::gm::Coord2D, te::gm::Coord2D> > map_inter;
1066 std::vector< te::sam::kdtree::Node< te::gm::Coord2D, GAP_pt_extern, GAP_pt_extern > *> reportsnode_int;
1068 std::vector<int32_t> pts_to_remove;
1070 for (std::vector< std::pair<size_t, te::gm::Polygon*> >::iterator it = pols.begin(); it != pols.end(); it++)
1073 int32_t polid = (int32_t)it->first;
1075 if (lin->getNPoints() < 4)
1077 for (
size_t i = 0; i < lin->getNPoints() - 1; i++)
1078 std::cout <<
"ERROR " << polid <<
" " << lin->
getPointN(i)->getX() <<
" " << lin->getPointN(i)->getY() << std::endl;
1085 for (
size_t i = 0; i < lin->getNPoints() - 1; i++)
1086 std::cout <<
"ERROR " << polid <<
" " << lin->getPointN(i)->
getX() <<
" " << lin->getPointN(i)->getY() << std::endl;
1093 std::cout <<
"ERROR " << polid <<
" area " << area << std::endl;
1101 for (i = 0; i < lin->getNPoints() - 1; i++)
1106 line->setCoord(0, first.getX(), first.getY());
1107 line->setCoord(1, last.getX(), last.getY());
1109 double dist01 =
Distance(first, last);
1113 te::gm::Envelope ept(line->getMBR()->getLowerLeftX() - m_dist_min, line->getMBR()->getLowerLeftY() - m_dist_min,
1114 line->getMBR()->getUpperRightX() + m_dist_min, line->getMBR()->getUpperRightY() + m_dist_min);
1115 m_nodetree->search(ept, reportsnode);
1117 for (
size_t j = 0; j < reportsnode.size(); j++)
1123 if (distsc == 0.)
continue;
1125 if (distsc < m_dist_min)
1128 if (reportsnode_int.size())
1130 if (distsc <= reportsnode_int[0]->getData().m_dist)
1133 if (fabs(distsc - reportsnode_int[0]->getData().m_dist) < m_tol
1134 && (i - reportsnode_int[0]->getData().m_seg_id) == 1)
1137 pts_to_remove.push_back((int32_t)i);
1142 reportsnode_int.clear();
1147 double dist0c =
Distance(first, c);
1155 if (pol->
disjoint(dynamic_cast<te::gm::Geometry*>(&pc)))
1158 reportsnode_int.clear();
1162 reportsnode.clear();
1166 for (i = 0; i < lin->getNPoints() - 1; i++)
1168 if (std::find(pts_to_remove.begin(), pts_to_remove.end(), i) != pts_to_remove.end())
1174 line->setCoord(0, first.getX(), first.getY());
1175 line->setCoord(1, last.getX(), last.getY());
1179 m_pol_points.push_back(
GAP_inter(polid, (int32_t)i, first.getX(), first.getY(), first.getX(), first.getY(), lin->getPointN(i)->getX(), lin->getPointN(i)->getY(),
1180 0., 0.,
"desf",
true));
1184 double dist01 =
Distance(first, last);
1186 te::gm::Envelope ept(line->getMBR()->getLowerLeftX() - m_dist_min, line->getMBR()->getLowerLeftY() - m_dist_min,
1187 line->getMBR()->getUpperRightX() + m_dist_min, line->getMBR()->getUpperRightY() + m_dist_min);
1188 kdtree_int->
search(ept, reportsnode_int);
1190 if (reportsnode_int.size())
1193 double k_dist = reportsnode_int[0]->getData().m_dist;
1194 std::vector<size_t> k_sel;
1197 for (
size_t j = 0; j < reportsnode_int.size(); j++)
1199 if (reportsnode_int[j]->getData().m_seg_id == (int32_t)i)
1202 double k_dist = reportsnode_int[j]->getData().m_dist;
1205 for (
size_t jj = j; jj < reportsnode_int.size(); jj++)
1207 if (
Distance(k_pint, reportsnode_int[jj]->getData().m_ptint) < m_tol)
1209 if (reportsnode_int[jj]->getData().m_dist > k_dist)
1211 k_dist = reportsnode_int[jj]->getData().m_dist;
1216 k_sel.push_back(j_sel);
1220 for (
size_t j = 0; j < k_sel.size(); j++)
1222 if (reportsnode_int[k_sel[j]]->getData().m_dist != 0)
1224 double dist0int =
Distance(first, reportsnode_int[k_sel[j]]->getData().m_ptint);
1225 if (dist0int < dist01)
1227 map_inter.insert(std::pair<
double, std::pair<te::gm::Coord2D, te::gm::Coord2D> >
1228 (dist0int, std::pair<te::gm::Coord2D, te::gm::Coord2D>(reportsnode_int[k_sel[j]]->getKey(), reportsnode_int[k_sel[j]]->getData().m_ptint)));
1234 for (std::map<
double, std::pair<te::gm::Coord2D, te::gm::Coord2D> >::iterator it = map_inter.begin(); it != map_inter.end(); it++)
1238 bool inside =
false;
1244 m_pol_points.push_back(
GAP_inter(polid, (int32_t)i, first.getX(), first.getY(), pint.
getX(), pint.
getY(), c.
getX(), c.
getY(),
1245 Distance(first, c), dist01,
"agreg", inside));
1249 reportsnode_int.clear();
1255 pts_to_remove.
clear();
1257 if (pt_out.size() < 3 || (pt_out.size() == 3 && pt_out[0] == pt_out[2]))
1260 std::cout <<
"ERROR " << polid << std::endl;
1267 for (pp = 0; pp < pt_out.size(); pp++)
1276 if (pol_ring->
size() <= 3)
1278 std::cout <<
"ERROR " << polid << std::endl;
1285 std::cout <<
"ERROR sem area " << polid << std::endl;
1294 std::cout <<
"Step3 - Divide " << polid << std::endl;
1297 polsout.push_back(std::pair<size_t, te::gm::Polygon*>(polid, dynamic_cast<te::gm::Polygon*>(mp->
getGeometryN(i))));
1300 polsout.push_back(std::pair<size_t, te::gm::Polygon*>(polid, pol_new));
1311 bool GAP::step4(std::vector< std::pair< size_t, te::gm::Polygon* > > &pols, std::vector< std::pair< size_t, te::gm::Polygon* > > &polsout)
1313 std::vector<te::gm::Line*> reportline, reportline_pol;
1314 std::vector<te::gm::Coord2D> pol_points;
1315 std::map<double, te::gm::Coord2D> map_inter;
1317 for (std::vector< std::pair<size_t, te::gm::Polygon*> >::iterator it = pols.begin(); it != pols.end(); it++)
1320 int32_t polid = (int32_t)it->first;
1325 if (area < (m_dist_min*m_dist_min) || !cent)
1327 std::cout <<
"Polygon ERROR " << polid <<
" area " << area << std::endl;
1332 Polygon2RTree(pol, rtree_pol);
1334 for (
size_t i = 0; i < lin->getNPoints() - 1; i++)
1338 double dist01 =
Distance(pt0, pt1);
1345 m_rtree.search(ept, reportline);
1347 double dist, dist_aux;
1348 if (!reportline.size())
1350 pol_points.push_back(pt0);
1352 0., 0.,
"desf",
true));
1360 dist =
coordToSegmentDistance(reportline[0]->getCoordinates()[0], reportline[0]->getCoordinates()[1], pt0, &ptint);
1363 for (
size_t rl = 1; rl < reportline.size(); rl++)
1365 dist_aux =
coordToSegmentDistance(reportline[rl]->getCoordinates()[0], reportline[rl]->getCoordinates()[1], pt0, &ptint_aux);
1366 if (dist_aux < dist)
1376 if (dist <= m_dist_min)
1378 double distpt0 =
Distance(pt0, reportline[seg_sel]->getCoordinates()[0]);
1379 double distpt1 =
Distance(pt0, reportline[seg_sel]->getCoordinates()[1]);
1380 if (distpt0 < distpt1 && distpt0 < m_dist_min)
1382 te::gm::Point pc(reportline[seg_sel]->getCoordinates()[0].x, reportline[seg_sel]->getCoordinates()[0].y, m_srid);
1383 if (pol->
disjoint(dynamic_cast<te::gm::Geometry*>(&pc)))
1388 rtree_pol.
search(ept, reportline_pol);
1389 bool temoutroseg =
false;
1390 for (
size_t pl = 0; pl < reportline_pol.size(); pl++)
1392 if (!(reportline_pol[pl]->getCoordinates()[0] == pt0) && !(reportline_pol[pl]->getCoordinates()[1] == pt0))
1394 if (!reportline_pol[pl]->disjoint(dynamic_cast<te::gm::Geometry*>(&segp0pint)))
1399 reportline_pol.clear();
1402 pol_points.push_back(reportline[seg_sel]->getCoordinates()[0]);
1403 m_pol_points.push_back(
GAP_inter(polid, (int32_t)i, pt0.
getX(), pt0.
getY(), ptint.
getX(), ptint.
getY(), reportline[seg_sel]->getCoordinates()[0].getX(), reportline[seg_sel]->getCoordinates()[0].getY(),
1404 dist, distpt0,
"agreg0",
true));
1409 else if (distpt1 < distpt0 && distpt1 < m_dist_min)
1411 te::gm::Point pc(reportline[seg_sel]->getCoordinates()[1].x, reportline[seg_sel]->getCoordinates()[1].y, m_srid);
1412 if (pol->
disjoint(dynamic_cast<te::gm::Geometry*>(&pc)))
1417 rtree_pol.
search(ept, reportline_pol);
1418 bool temoutroseg =
false;
1419 for (
size_t pl = 0; pl < reportline_pol.size(); pl++)
1421 if (!(reportline_pol[pl]->getCoordinates()[0] == pt0) && !(reportline_pol[pl]->getCoordinates()[1] == pt0))
1423 if (!reportline_pol[pl]->disjoint(dynamic_cast<te::gm::Geometry*>(&segp0pint)))
1428 reportline_pol.clear();
1431 pol_points.push_back(reportline[seg_sel]->getCoordinates()[1]);
1432 m_pol_points.push_back(
GAP_inter(polid, (int32_t)i, pt0.
getX(), pt0.
getY(), ptint.
getX(), ptint.
getY(), reportline[seg_sel]->getCoordinates()[1].getX(), reportline[seg_sel]->getCoordinates()[1].getY(),
1433 dist, distpt1,
"agreg1",
true));
1440 double distpt0_int =
Distance(pt0, ptint);
1441 if (distpt0_int < m_dist_min)
1445 if (pointLocate(ptint, pol) != geos::geom::Location::INTERIOR)
1454 rtree_pol.
search(ept, reportline_pol);
1455 bool temoutroseg =
false;
1456 for (
size_t pl = 0; pl < reportline_pol.size(); pl++)
1458 if (!(reportline_pol[pl]->getCoordinates()[0] == pt0) && !(reportline_pol[pl]->getCoordinates()[1] == pt0))
1460 if (reportline_pol[pl]->intersects(dynamic_cast<te::gm::Geometry*>(&segp0pint)))
1465 reportline_pol.clear();
1468 pol_points.push_back(ptint);
1470 dist, 0.,
"inter",
true));
1476 if (
segInterPoint(reportline[seg_sel]->getCoordinates()[0], reportline[seg_sel]->getCoordinates()[1], pt0,
pt1, &ptint))
1480 bool temoutroseg =
false;
1481 for (
size_t pl = 0; pl < reportline_pol.size(); pl++)
1483 if (!(reportline_pol[pl]->getCoordinates()[0] == pt0) && !(reportline_pol[pl]->getCoordinates()[1] == pt0))
1485 if (!reportline_pol[pl]->disjoint(dynamic_cast<te::gm::Geometry*>(&segp0pint)))
1490 reportline_pol.clear();
1493 pol_points.push_back(ptint);
1495 dist, 0.,
"inter",
true));
1505 pol_points.push_back(pt0);
1507 0., 0.,
"desf",
true));
1515 if (pol_points.size() < 3)
1517 std::cout <<
"Step4 - out Polygon ERROR " << polid << std::endl;
1525 for (pp = 0; pp < pol_points.size(); pp++)
1535 std::cout <<
"Step4 - ERROR " << polid << std::endl;
1539 polsout.
push_back(std::pair<int32_t, te::gm::Polygon *>(polid, pol_new));
1547 bool GAP::SavePol(std::vector< std::pair<size_t, te::gm::Polygon*> >& pols, std::string step)
1549 std::string pol(
"poly_all");
1550 std::string pt(
"point_all");
1551 std::string dir(
"D:/Dados_GAP/");
1552 m_filenameout = dir + pol + step +
".shp";
1553 m_inDSout = pol + step;
1554 std::string connInfo(
"file://");
1555 connInfo += m_filenameout;
1560 if (Dspol->dataSetExists(polDS))
1562 std::cout <<
"A dataset with the same requested output dataset name already exists: " << polDS << std::endl;
1581 std::string connInfo(
"file://");
1582 std::string filenamept = dir + pt + step +
".shp";
1583 connInfo += filenamept;
1590 if (Dspt->dataSetExists(ptDS))
1592 std::cout <<
"A dataset with the same requested output dataset name already exists: " << ptDS << std::endl;
1615 dtpt->add(propptid);
1616 dtpt->add(propdist0);
1617 dtpt->add(propdist00);
1630 for (std::vector< std::pair<size_t, te::gm::Polygon*> >::iterator it = pols.begin(); it != pols.end(); it++)
1634 dataSetItem->
setInt32(
"FID",
id++);
1635 dataSetItem->
setInt32(
"FIDoriginal", (int32_t)it->first);
1636 dataSetItem->
setDouble(
"Areaoriginal", m_original_area[(int32_t)it->first]);
1638 dataSetItem->
setGeometry(
"geometry", dynamic_cast<te::gm::Geometry*>(pol->
clone()));
1639 dsp->
add(dataSetItem);
1645 for (
size_t pp = 0; pp < m_pol_points.size(); pp++)
1648 dataSetItem->
setInt32(
"FID", (int32_t)pp);
1649 dataSetItem->
setInt32(
"POLID", m_pol_points[pp].m_polid);
1650 dataSetItem->
setInt32(
"ptorder", m_pol_points[pp].m_ptid);
1651 dataSetItem->
setDouble(
"dist0", m_pol_points[pp].m_distance);
1652 dataSetItem->
setDouble(
"dist00", m_pol_points[pp].m_distance0);
1653 dataSetItem->
setDouble(
"x", m_pol_points[pp].m_x);
1654 dataSetItem->
setDouble(
"y", m_pol_points[pp].m_y);
1655 dataSetItem->
setDouble(
"xint", m_pol_points[pp].m_xint);
1656 dataSetItem->
setDouble(
"yint", m_pol_points[pp].m_yint);
1657 dataSetItem->
setDouble(
"newx", m_pol_points[pp].m_xnew);
1658 dataSetItem->
setDouble(
"newy", m_pol_points[pp].m_ynew);
1659 dataSetItem->
setString(
"tipo", m_pol_points[pp].m_tipo);
1660 dataSetItem->
setInt32(
"inside", (m_pol_points[pp].m_inside) ? 0 : 1);
1661 dataSetItem->
setGeometry(
"pt", dynamic_cast<te::gm::Geometry*>(
new te::gm::Point(m_pol_points[pp].m_xnew, m_pol_points[pp].m_ynew)));
1662 dspt->
add(dataSetItem);
1664 Save(Dspol.get(), dsp, dtp.get());
1665 Save(Dspt.get(), dspt, dtpt.get());
1681 double x0 = pt_out[0].x;
1682 double y0 = pt_out[0].y;
1684 for (
size_t i = 0; i < pt_out.size() - 1; i++)
1686 l0.
setCoord(0, pt_out[i].x, pt_out[i].y);
1687 l0.
setCoord(1, pt_out[i+1].x, pt_out[i+1].y);
1688 for (
size_t ii = i; ii < pt_out.size() - 1; ii++)
1690 l1.
setCoord(0, pt_out[ii].x, pt_out[ii].y);
1691 l1.
setCoord(1, pt_out[ii + 1].x, pt_out[ii + 1].y);
1696 pint.
getX() == x0 && pint.
getY() == y0)
1700 for (
size_t j = i + 1; (j < ii && j < pt_out.size()-1); j++)
1701 pt_out.erase(pt_out.begin() + j);
1711 std::vector<te::gm::Line*> reportline_pol;
1712 bool temoutroseg =
false;
1718 for (
size_t pl = 0; pl < reportline_pol.size(); pl++)
1720 if (!(reportline_pol[pl]->getCoordinates()[0] == pt0) && !(reportline_pol[pl]->getCoordinates()[1] == pt0))
1722 if (reportline_pol[pl]->intersects(dynamic_cast<te::gm::Geometry*>(&segp0pint)))
1728 if (pint_agreg == p0 || pint_agreg == p1)
1742 reportline_pol.clear();
1750 const geos::geom::Coordinate
p(pt.
x, pt.
y);
1752 geos::geom::GeometryFactory factory;
1753 geos::geom::Point* geos_pt = factory.createPoint(p);
1754 double dist = geos_pol->getBoundary()->distance(geos_pt);
1757 return geos::geom::Location::BOUNDARY;
1759 geos::algorithm::PointLocator loc;
1760 return loc.locate(p, geos_pol.get());
void insert(const kdKey &key, const kdDataItem &item)
It inserts the data with a given key in tree.
std::size_t getNumRings() const
It returns the number of rings in this CurvePolygon.
void setAutoNumber(bool a)
It tells if the property is an autonumber or not.
std::size_t getNumGeometries() const
It returns the number of geometries in this GeometryCollection.
int pointLocate(te::gm::Coord2D &pt, te::gm::Polygon *pol)
TEXSDEXPORT void Save(All *all, te::xml::AbstractWriter &writer)
void push_back(Curve *ring)
It adds the curve to the curve polygon.
MultiPolygon is a MultiSurface whose elements are Polygons.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
double coordToSegmentDistance(te::gm::Coord2D &fseg, te::gm::Coord2D &lseg, te::gm::Coord2D &pt, te::gm::Coord2D *pti)
void setGeometry(std::size_t i, te::gm::Geometry *value)
It sets the value of the i-th property.
A Line is LineString with 2 points.
virtual std::unique_ptr< DataSourceTransactor > getTransactor()=0
It returns the set of parameters used to set up the access channel to the underlying repository...
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
void setDouble(std::size_t i, double value)
It sets the value of the i-th property.
An atomic property like an integer or double.
te::gm::Coord2D appGetCenterPointOfPoints(std::vector< te::gm::Coord2D > &pts)
void setSRID(int srid)
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
bool step2(std::vector< std::pair< size_t, te::gm::Polygon * > > &pols, std::vector< std::pair< size_t, te::gm::Polygon * > > &polsout)
boost::shared_ptr< DataSource > DataSourcePtr
Coord2D * getCoordinates() const
It returns a pointer to the internal array of coordinates.
bool step3(std::vector< std::pair< size_t, te::gm::Polygon * > > &pols, std::vector< std::pair< size_t, te::gm::Polygon * > > &polsout)
static te::dt::Date dx(2010, 12, 31)
bool step00(std::vector< std::pair< size_t, te::gm::Polygon * > > &polsout)
A class that models the description of a dataset.
te::gm::LineString * GEOS_DouglasPeucker(te::gm::LineString *ls, double snap)
virtual double getLength() const
The length of this curve in the unit associated to its spatial reference system.
virtual void createDataSet(DataSetType *dt, const std::map< std::string, std::string > &options)
It creates the dataset schema definition in the target data source.
GeomType getGeomTypeId() const _NOEXCEPT_OP(true)
It returns the geometry subclass type identifier.
bool step1(std::vector< std::pair< size_t, te::gm::Polygon * > > &pols, std::vector< std::pair< size_t, te::gm::Polygon * > > &polsout)
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
Curve * getExteriorRing() const
It returns the exterior ring of this CurvePolygon.
TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet *dataset, const std::string &name)
virtual void add(const std::string &datasetName, DataSet *d, const std::map< std::string, std::string > &options, std::size_t limit=0)
It adds data items to the dataset in the data source.
std::unique_ptr< Point > getPointN(std::size_t i) const
It returns the specified point in this LineString.
virtual bool disjoint(const Geometry *const rhs) const _NOEXCEPT_OP(false)
It returns true if the geometry object is spatially disjoint from rhs geometry.
bool intersection(const Line &line, Point &coord) const
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
bool Polygon2Points(te::gm::Polygon *pol, int32_t id)
An utility struct for representing 2D coordinates.
double getY() const
It returns the y-coordinate.
bool LoadPolygons(std::string &filename, std::string &inDsetName, Pol_OUT out)
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
bool isClosed() const
It returns true if the curve is closed (startPoint = endPoint).
void add(DataSetItem *item)
It adds a new item to the dataset and takes its ownership.
virtual std::string getType() const =0
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
virtual te::dt::AbstractData * clone() const
It clones the linestring.
void setInt32(std::size_t i, boost::int32_t value)
It sets the value of the i-th property.
A LinearRing is a LineString that is both closed and simple.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
bool operator==(const GAP_inter &)
int getSRID() const _NOEXCEPT_OP(true)
It returns the Spatial Reference System ID associated to this geometric object.
LineString is a curve with linear interpolation between points.
const double & getY() const
It returns the Point y-coordinate value.
A point with x and y coordinate values.
const Envelope * getMBR() const _NOEXCEPT_OP(true)
It returns the minimum bounding rectangle for the geometry in an internal representation.
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
An Envelope defines a 2D rectangular region.
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
bool step0(std::vector< std::pair< size_t, te::gm::Polygon * > > &pols, std::vector< std::pair< size_t, te::gm::Polygon * > > &polsout)
void verify_polygon(std::vector< te::gm::Coord2D > &pt_out)
void setCoord(int index, double x, double y, double z=0., double m=0.)
float coef_angular(double fx, double fy, double lx, double ly)
std::size_t getNPoints() const
It returns the number of points (vertexes) in the linestring.
A class that represents a two dimensional K-d Tree (2-d Tree).
int search(const te::gm::Envelope &mbr, std::vector< DATATYPE > &report) const
Range search query.
bool isInside(te::gm::Coord2D &first, te::gm::Coord2D &last, te::gm::Coord2D &c, te::gm::Coord2D ¢)
double SegmentDistance(double fx, double fy, double lx, double ly, double ptx, double pty, double *pix, double *piy)
Function that evaluates the distance between a point and a segment.
static geos::geom::Geometry * write(const Geometry *teGeom)
It reads a TerraLib geometry and make a GEOS geometry.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
A class that converts a TerraLib geometry to a GEOS geometry.
bool Polygon2RTree(te::gm::Polygon *pol, te::sam::rtree::Index< te::gm::Line * > &rtree)
double getArea() const
It returns the area of this surface, as measured in the spatial reference system of this surface...
double Distance(const double pt1x, const double pt1y, const double pt2x, const double pt2y)
Geometry * getGeometryN(std::size_t i) const
It returns the n-th geometry in this GeometryCollection.
bool verifyIntersections(te::gm::Coord2D &pt0, te::gm::Coord2D &pc, te::sam::rtree::Index< te::gm::Line * > &rtree_pol)
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
A dataset is the unit of information manipulated by the data access module of TerraLib.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
double getX() const
It returns the x-coordinate.
te::sam::kdtree::Index< KD_NODE > KD_TREE
void insert(const te::gm::Envelope &mbr, const DATATYPE &data)
It inserts an item into the tree.
void clear()
It clears all tree nodes.
void insert_inter(GAP_inter *inter)
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
Coord2D * getCentroidCoord() const
It returns the mathematical centroid for this surface as a coordinate.
bool Polygon2Lines(te::gm::Polygon *pol)
bool operator<(const GAP_inter &)
void setPointN(std::size_t i, const Point &p)
It sets the value of the specified point to this new one.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
void setString(std::size_t i, const std::string &value)
It sets the value of the i-th property.
void search(const te::gm::Envelope &e, std::vector< KdTreeNode * > &report) const
Range search query.
bool SavePol(std::vector< std::pair< size_t, te::gm::Polygon * > > &pols, std::string step)
bool operator()(te::gm::Coord2D &a, te::gm::Coord2D &b)
bool step4(std::vector< std::pair< size_t, te::gm::Polygon * > > &pols, std::vector< std::pair< size_t, te::gm::Polygon * > > &polsout)
bool isInside(te::gm::Coord2D &pt)
void Save(te::da::DataSource *source, te::da::DataSet *result, te::da::DataSetType *outDsType)
bool segInterPoint(te::gm::Coord2D &pfr, te::gm::Coord2D &pto, te::gm::Coord2D &lfr, te::gm::Coord2D <o, te::gm::Coord2D *pt)
const double & getX() const
It returns the Point x-coordinate value.
TEGEOMEXPORT te::gm::Geometry * Validate(te::gm::Geometry *geom)
Get/create a valid version of the geometry given. If the geometry is a polygon or multi polygon...
sortpoints(te::gm::Coord2D &pt)
Curve * getRingN(std::size_t i) const
It returns the n-th ring for this curve polygon as a curve.
std::size_t size() const
It returns the number of points (vertexes) in the geometry.
const std::string & getName() const
It returns the property name.