Loading...
Searching...
No Matches
multilinestring_range.cuh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022-2025, NVIDIA CORPORATION.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cuspatial/cuda_utils.hpp>
20#include <cuspatial/detail/range/enumerate_range.cuh>
23#include <cuspatial/traits.hpp>
24#include <cuspatial/types.hpp>
25
26#include <rmm/cuda_stream_view.hpp>
27
28#include <cuda/std/optional>
29#include <thrust/pair.h>
30
31namespace cuspatial {
32
36
59template <typename GeometryIterator, typename PartIterator, typename VecIterator>
60class multilinestring_range {
61 public:
62 using geometry_it_t = GeometryIterator;
63 using part_it_t = PartIterator;
64 using point_it_t = VecIterator;
65 using point_t = iterator_value_type<VecIterator>;
66 using element_t = iterator_vec_base_type<VecIterator>;
67
68 CUSPATIAL_HOST_DEVICE multilinestring_range(GeometryIterator geometry_begin,
69 GeometryIterator geometry_end,
70 PartIterator part_begin,
71 PartIterator part_end,
72 VecIterator points_begin,
73 VecIterator points_end);
74
76 CUSPATIAL_HOST_DEVICE auto size() { return num_multilinestrings(); }
77
79 CUSPATIAL_HOST_DEVICE auto num_multilinestrings();
80
82 CUSPATIAL_HOST_DEVICE auto num_linestrings();
83
85 CUSPATIAL_HOST_DEVICE auto num_points();
86
88 CUSPATIAL_HOST_DEVICE auto multilinestring_begin();
89
91 CUSPATIAL_HOST_DEVICE auto multilinestring_end();
92
94 CUSPATIAL_HOST_DEVICE auto begin() { return multilinestring_begin(); }
95
97 CUSPATIAL_HOST_DEVICE auto end() { return multilinestring_end(); }
98
100 CUSPATIAL_HOST_DEVICE auto point_begin() { return _point_begin; }
101
103 CUSPATIAL_HOST_DEVICE auto point_end() { return _point_end; }
104
106 CUSPATIAL_HOST_DEVICE auto geometry_offset_begin() { return _geometry_begin; }
107
109 CUSPATIAL_HOST_DEVICE auto geometry_offset_end() { return _geometry_end; }
110
112 CUSPATIAL_HOST_DEVICE auto part_offset_begin() { return _part_begin; }
113
115 CUSPATIAL_HOST_DEVICE auto part_offset_end() { return _part_end; }
116
118 template <typename IndexType>
119 CUSPATIAL_HOST_DEVICE auto part_idx_from_point_idx(IndexType point_idx);
120
123 template <typename IndexType>
124 CUSPATIAL_HOST_DEVICE
125 cuda::std::optional<typename thrust::iterator_traits<PartIterator>::difference_type>
126 part_idx_from_segment_idx(IndexType point_idx);
127
130 template <typename IndexType>
131 CUSPATIAL_HOST_DEVICE auto geometry_idx_from_part_idx(IndexType part_idx);
132
135 template <typename IndexType>
136 CUSPATIAL_HOST_DEVICE auto geometry_idx_from_point_idx(IndexType point_idx);
137
138 // Given index to a linestring, return the index of the linestring inside its multilinestring.
139 template <typename IndexType>
140 CUSPATIAL_HOST_DEVICE auto intra_part_idx(IndexType global_part_idx);
141
142 // Given index to a point, return the index of the point inside its linestring.
143 template <typename IndexType>
144 CUSPATIAL_HOST_DEVICE auto intra_point_idx(IndexType global_point_idx);
145
149 template <typename IndexType1, typename IndexType2>
150 CUSPATIAL_HOST_DEVICE bool is_valid_segment_id(IndexType1 segment_idx, IndexType2 part_idx);
151
153 template <typename IndexType>
154 CUSPATIAL_HOST_DEVICE auto segment(IndexType segment_idx);
155
157 CUSPATIAL_HOST_DEVICE auto multilinestring_point_count_begin();
158
160 CUSPATIAL_HOST_DEVICE auto multilinestring_point_count_end();
161
163 CUSPATIAL_HOST_DEVICE auto multilinestring_linestring_count_begin();
164
166 CUSPATIAL_HOST_DEVICE auto multilinestring_linestring_count_end();
167
171 auto _segments(rmm::cuda_stream_view);
172
174 template <typename IndexType>
175 CUSPATIAL_HOST_DEVICE auto operator[](IndexType multilinestring_idx);
176
178
182 CUSPATIAL_HOST_DEVICE auto as_multipoint_range();
183
184 protected:
185 GeometryIterator _geometry_begin;
186 GeometryIterator _geometry_end;
187 PartIterator _part_begin;
188 PartIterator _part_end;
189 VecIterator _point_begin;
190 VecIterator _point_end;
191
192 private:
195 template <typename IndexType>
196 CUSPATIAL_HOST_DEVICE auto _part_iter_from_point_idx(IndexType point_idx);
199 template <typename IndexType>
200 CUSPATIAL_HOST_DEVICE auto _geometry_iter_from_part_idx(IndexType part_idx);
201};
202
230template <typename GeometryIteratorDiffType,
231 typename PartIteratorDiffType,
232 typename VecIteratorDiffType,
233 typename GeometryIterator,
234 typename PartIterator,
235 typename VecIterator>
236auto make_multilinestring_range(GeometryIteratorDiffType num_multilinestrings,
237 GeometryIterator geometry_begin,
238 PartIteratorDiffType num_linestrings,
239 PartIterator part_begin,
240 VecIteratorDiffType num_points,
241 VecIterator point_begin)
242{
243 return multilinestring_range{geometry_begin,
244 geometry_begin + num_multilinestrings + 1,
245 part_begin,
246 part_begin + num_linestrings + 1,
247 point_begin,
248 point_begin + num_points};
249}
250
263template <typename IntegerRange1, typename IntegerRange2, typename PointRange>
264auto make_multilinestring_range(IntegerRange1 geometry_offsets,
265 IntegerRange2 part_offsets,
266 PointRange points)
267{
268 return multilinestring_range(geometry_offsets.begin(),
269 geometry_offsets.end(),
270 part_offsets.begin(),
271 part_offsets.end(),
272 points.begin(),
273 points.end());
274}
275
282template <collection_type_id Type,
283 typename T,
284 typename IndexType,
285 typename GeometryColumnView,
286 CUSPATIAL_ENABLE_IF(Type == collection_type_id::SINGLE)>
287auto make_multilinestring_range(GeometryColumnView const& linestrings_column)
288{
289 CUSPATIAL_EXPECTS(linestrings_column.geometry_type() == geometry_type_id::LINESTRING,
290 "Must be Linestring geometry type.");
291 auto geometry_iter = thrust::make_counting_iterator(0);
292 auto const& part_offsets = linestrings_column.offsets();
293 auto const& points_xy = linestrings_column.child().child(1); // Ignores x-y offset {0, 2, 4...}
294
295 auto points_it = make_vec_2d_iterator(points_xy.template begin<T>());
296
297 return multilinestring_range(geometry_iter,
298 geometry_iter + part_offsets.size(),
299 part_offsets.template begin<IndexType>(),
300 part_offsets.template end<IndexType>(),
301 points_it,
302 points_it + points_xy.size() / 2);
303}
304
311template <collection_type_id Type,
312 typename T,
313 typename IndexType,
314 CUSPATIAL_ENABLE_IF(Type == collection_type_id::MULTI),
315 typename GeometryColumnView>
316auto make_multilinestring_range(GeometryColumnView const& linestrings_column)
317{
318 CUSPATIAL_EXPECTS(linestrings_column.geometry_type() == geometry_type_id::LINESTRING,
319 "Must be Linestring geometry type.");
320 auto const& geometry_offsets = linestrings_column.offsets();
321 auto const& parts = linestrings_column.child();
322 auto const& part_offsets = parts.child(0);
323 auto const& points_xy = parts.child(1).child(1); // Ignores x-y offset {0, 2, 4...}
324
325 auto points_it = make_vec_2d_iterator(points_xy.template begin<T>());
326
327 return multilinestring_range(geometry_offsets.template begin<IndexType>(),
328 geometry_offsets.template end<IndexType>(),
329 part_offsets.template begin<IndexType>(),
330 part_offsets.template end<IndexType>(),
331 points_it,
332 points_it + points_xy.size() / 2);
333};
334
338
339} // namespace cuspatial
340
341#include <cuspatial/detail/range/multilinestring_range.cuh>
Non-owning range-based interface to multilinestring data.
CUSPATIAL_HOST_DEVICE auto multilinestring_point_count_begin()
Returns an iterator to the counts of points per multilinestring.
CUSPATIAL_HOST_DEVICE auto end()
Return the iterator to the one past the last multilinestring in the range.
CUSPATIAL_HOST_DEVICE auto multilinestring_linestring_count_end()
Returns an iterator to the counts of points per multilinestring.
CUSPATIAL_HOST_DEVICE cuda::std::optional< typename thrust::iterator_traits< PartIterator >::difference_type > part_idx_from_segment_idx(IndexType point_idx)
CUSPATIAL_HOST_DEVICE auto size()
Return the number of multilinestrings in the array.
CUSPATIAL_HOST_DEVICE auto part_offset_end()
Return the iterator to the one past the last part offset in the range.
CUSPATIAL_HOST_DEVICE auto multilinestring_end()
Return the iterator to the one past the last multilinestring in the range.
CUSPATIAL_HOST_DEVICE auto geometry_idx_from_point_idx(IndexType point_idx)
CUSPATIAL_HOST_DEVICE auto part_idx_from_point_idx(IndexType point_idx)
Given the index of a point, return the part (linestring) index where the point locates.
CUSPATIAL_HOST_DEVICE bool is_valid_segment_id(IndexType1 segment_idx, IndexType2 part_idx)
CUSPATIAL_HOST_DEVICE auto geometry_offset_end()
Return the iterator to the one past the last geometry offset in the range.
CUSPATIAL_HOST_DEVICE auto multilinestring_linestring_count_begin()
Returns an iterator to the counts of points per multilinestring.
CUSPATIAL_HOST_DEVICE auto segment(IndexType segment_idx)
Returns the segment given a segment index.
CUSPATIAL_HOST_DEVICE auto point_end()
Return the iterator to the one past the last point in the range.
CUSPATIAL_HOST_DEVICE auto num_multilinestrings()
Return the number of multilinestrings in the array.
CUSPATIAL_HOST_DEVICE auto multilinestring_point_count_end()
Returns an iterator to the counts of segments per multilinestring.
CUSPATIAL_HOST_DEVICE auto geometry_idx_from_part_idx(IndexType part_idx)
CUSPATIAL_HOST_DEVICE auto geometry_offset_begin()
Return the iterator to the first geometry offset in the range.
CUSPATIAL_HOST_DEVICE auto as_multipoint_range()
Range Casts.
CUSPATIAL_HOST_DEVICE auto point_begin()
Return the iterator to the first point in the range.
CUSPATIAL_HOST_DEVICE auto operator[](IndexType multilinestring_idx)
Returns the multilinestring_idxth multilinestring in the range.
CUSPATIAL_HOST_DEVICE auto num_points()
Return the total number of points in the array.
CUSPATIAL_HOST_DEVICE auto multilinestring_begin()
Return the iterator to the first multilinestring in the range.
CUSPATIAL_HOST_DEVICE auto num_linestrings()
Return the total number of linestrings in the array.
CUSPATIAL_HOST_DEVICE auto begin()
Return the iterator to the first multilinestring in the range.
CUSPATIAL_HOST_DEVICE auto part_offset_begin()
Return the iterator to the first part offset in the range.
#define CUSPATIAL_EXPECTS(cond, reason)
Macro for checking (pre-)conditions that throws an exception when a condition is violated.
Definition error.hpp:76
auto make_vec_2d_iterator(FirstIter first, SecondIter second)
Create an iterator to vec_2d data from two input iterators.
auto make_multilinestring_range(GeometryIteratorDiffType num_multilinestrings, GeometryIterator geometry_begin, PartIteratorDiffType num_linestrings, PartIterator part_begin, VecIteratorDiffType num_points, VecIterator point_begin)
Create a multilinestring_range object from size and start iterators.