|
| 1 | +// Python Source: shapely/geometry/linestring.py |
| 2 | +// Line Range: class LineString method implementations |
| 3 | +// Alignment: strict |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include "shapely/geometry/point.h" |
| 8 | +#include "shapely/geometry/polygon.h" |
| 9 | + |
| 10 | +#include <geos/geom/GeometryFactory.h> |
| 11 | +#include <geos/geom/LineString.h> |
| 12 | +#include <geos/geom/Point.h> |
| 13 | +#include <geos/geom/Polygon.h> |
| 14 | +#include <geos/geom/Coordinate.h> |
| 15 | +#include <geos/geom/CoordinateSequence.h> |
| 16 | +#include <geos/geom/CoordinateSequenceFactory.h> |
| 17 | +#include <geos/operation/distance/DistanceOp.h> |
| 18 | +#include <geos/linearref/LengthIndexedLine.h> |
| 19 | +#include <stdexcept> |
| 20 | + |
| 21 | +namespace shapely { |
| 22 | +namespace geometry { |
| 23 | + |
| 24 | +template <typename T> |
| 25 | +LineString<T>::LineString(const py::array_t<T>& coords) |
| 26 | + : coords_(coords) |
| 27 | +{ |
| 28 | + auto buf = coords.request(); |
| 29 | + if (buf.ndim != 2 || buf.shape[1] < 2) |
| 30 | + throw std::runtime_error("LineString: coords must be [N, 2] or [N, 3] array"); |
| 31 | + if (buf.shape[0] < 2) |
| 32 | + throw std::runtime_error("LineString: must have at least 2 points"); |
| 33 | + |
| 34 | + factory_ = geos::geom::GeometryFactory::create(); |
| 35 | + T *p = static_cast<T *>(buf.ptr); |
| 36 | + size_t n = buf.shape[0]; |
| 37 | + int stride = buf.shape[1]; |
| 38 | + |
| 39 | + auto coord_seq = factory_->getCoordinateSequenceFactory()->create(n, 2); |
| 40 | + for (size_t i = 0; i < n; ++i) |
| 41 | + coord_seq->setAt(geos::geom::Coordinate( |
| 42 | + static_cast<double>(p[i * stride + 0]), |
| 43 | + static_cast<double>(p[i * stride + 1])), i); |
| 44 | + |
| 45 | + geos_linestring_ = factory_->createLineString(std::move(coord_seq)); |
| 46 | +} |
| 47 | + |
| 48 | +template <typename T> |
| 49 | +double LineString<T>::distance(const LineString& other) const |
| 50 | +{ |
| 51 | + geos::operation::distance::DistanceOp dist_op(geos_linestring_.get(), other.geos_linestring_.get()); |
| 52 | + return dist_op.distance(); |
| 53 | +} |
| 54 | + |
| 55 | +template <typename T> |
| 56 | +template <typename U> |
| 57 | +double LineString<T>::distance(const Polygon<U>& other) const |
| 58 | +{ |
| 59 | + geos::operation::distance::DistanceOp dist_op(geos_linestring_.get(), other.geos_polygon_.get()); |
| 60 | + return dist_op.distance(); |
| 61 | +} |
| 62 | + |
| 63 | +template <typename T> |
| 64 | +template <typename U> |
| 65 | +double LineString<T>::distance(const Point<U>& other) const |
| 66 | +{ |
| 67 | + geos::operation::distance::DistanceOp dist_op(geos_linestring_.get(), other.geos_point_.get()); |
| 68 | + return dist_op.distance(); |
| 69 | +} |
| 70 | + |
| 71 | +template <typename T> |
| 72 | +template <typename U> |
| 73 | +bool LineString<T>::intersects(const Polygon<U>& other) const |
| 74 | +{ |
| 75 | + return geos_linestring_->intersects(other.geos_polygon_.get()); |
| 76 | +} |
| 77 | + |
| 78 | +template <typename T> |
| 79 | +template <typename U> |
| 80 | +double LineString<T>::project(const Point<U>& other) const |
| 81 | +{ |
| 82 | + geos::linearref::LengthIndexedLine indexed_line(geos_linestring_.get()); |
| 83 | + return indexed_line.project(geos::geom::Coordinate( |
| 84 | + static_cast<double>(other.x_), static_cast<double>(other.y_))); |
| 85 | +} |
| 86 | + |
| 87 | +template <typename T> |
| 88 | +Point<double> LineString<T>::interpolate(double distance) const |
| 89 | +{ |
| 90 | + geos::linearref::LengthIndexedLine indexed_line(geos_linestring_.get()); |
| 91 | + auto coord = indexed_line.extractPoint(distance); |
| 92 | + return Point<double>(coord.x, coord.y); |
| 93 | +} |
| 94 | + |
| 95 | +template <typename T> |
| 96 | +double LineString<T>::length() const |
| 97 | +{ |
| 98 | + return geos_linestring_->getLength(); |
| 99 | +} |
| 100 | + |
| 101 | +} // namespace geometry |
| 102 | +} // namespace shapely |
0 commit comments