Skip to content

Commit 439fcdb

Browse files
thomcomtrxcllnt
andauthored
Link createLonLatToCartesian in @rapidsai/cuspatial (#440)
* Create createLonLatToCartesian * Add the missing test file and a little cleanup. * Really need that file. * Refactor based on Paul's comments. * Update modules/cuspatial/src/node_cuspatial.ts Co-authored-by: Paul Taylor <paul.e.taylor@me.com> * Update modules/cuspatial/test/spatial-tests.ts Co-authored-by: Paul Taylor <paul.e.taylor@me.com> * Update modules/cuspatial/src/spatial.ts Co-authored-by: Paul Taylor <paul.e.taylor@me.com> * fix tsc compilation errors * add optional memoryResource argument to convertLonLatToCartesian * update copyright year Co-authored-by: Paul Taylor <paul.e.taylor@me.com>
1 parent 5e951d5 commit 439fcdb

8 files changed

Lines changed: 123 additions & 15 deletions

File tree

modules/cuspatial/src/addon.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021, NVIDIA CORPORATION.
1+
// Copyright (c) 2021-2022, NVIDIA CORPORATION.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -21,19 +21,20 @@ struct rapidsai_cuspatial : public nv::EnvLocalAddon, public Napi::Addon<rapidsa
2121
rapidsai_cuspatial(Napi::Env const& env, Napi::Object exports) : nv::EnvLocalAddon(env, exports) {
2222
DefineAddon(
2323
exports,
24-
{
25-
InstanceMethod("init", &rapidsai_cuspatial::InitAddon),
26-
InstanceValue("_cpp_exports", _cpp_exports.Value()),
27-
InstanceMethod<&rapidsai_cuspatial::create_quadtree>("createQuadtree"),
28-
InstanceMethod<&rapidsai_cuspatial::quadtree_bounding_box_intersections>(
29-
"findQuadtreeAndBoundingBoxIntersections"),
30-
InstanceMethod<&rapidsai_cuspatial::find_points_in_polygons>("findPointsInPolygons"),
31-
InstanceMethod<&rapidsai_cuspatial::find_polyline_nearest_to_each_point>(
32-
"findPolylineNearestToEachPoint"),
33-
InstanceMethod<&rapidsai_cuspatial::compute_polygon_bounding_boxes>(
34-
"computePolygonBoundingBoxes"),
35-
InstanceMethod<&rapidsai_cuspatial::compute_polyline_bounding_boxes>(
36-
"computePolylineBoundingBoxes"),
24+
{InstanceMethod("init", &rapidsai_cuspatial::InitAddon),
25+
InstanceValue("_cpp_exports", _cpp_exports.Value()),
26+
InstanceMethod<&rapidsai_cuspatial::create_quadtree>("createQuadtree"),
27+
InstanceMethod<&rapidsai_cuspatial::quadtree_bounding_box_intersections>(
28+
"findQuadtreeAndBoundingBoxIntersections"),
29+
InstanceMethod<&rapidsai_cuspatial::find_points_in_polygons>("findPointsInPolygons"),
30+
InstanceMethod<&rapidsai_cuspatial::find_polyline_nearest_to_each_point>(
31+
"findPolylineNearestToEachPoint"),
32+
InstanceMethod<&rapidsai_cuspatial::compute_polygon_bounding_boxes>(
33+
"computePolygonBoundingBoxes"),
34+
InstanceMethod<&rapidsai_cuspatial::compute_polyline_bounding_boxes>(
35+
"computePolylineBoundingBoxes"),
36+
InstanceMethod<&rapidsai_cuspatial::lonlat_to_cartesian>("lonLatToCartesian")
37+
3738
});
3839
}
3940

@@ -54,6 +55,9 @@ struct rapidsai_cuspatial : public nv::EnvLocalAddon, public Napi::Addon<rapidsa
5455
Napi::Value compute_polyline_bounding_boxes(Napi::CallbackInfo const& info) {
5556
return nv::compute_polyline_bounding_boxes(info);
5657
}
58+
Napi::Value lonlat_to_cartesian(Napi::CallbackInfo const& info) {
59+
return nv::lonlat_to_cartesian(info);
60+
}
5761
};
5862

5963
NODE_API_ADDON(rapidsai_cuspatial);

modules/cuspatial/src/addon.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const {
2626
computePolylineBoundingBoxes,
2727
findPointsInPolygons,
2828
findPolylineNearestToEachPoint,
29+
lonLatToCartesian,
2930
_cpp_exports,
3031
} = require('bindings')('rapidsai_cuspatial.node').init(CORE, CUDA, RMM, CUDF) as
3132
typeof import('./node_cuspatial');

modules/cuspatial/src/geometry.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021, NVIDIA CORPORATION.
1+
// Copyright (c) 2021-2022, NVIDIA CORPORATION.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919

2020
#include <node_rmm/utilities/napi_to_cpp.hpp>
2121

22+
#include <cuspatial/coordinate_transform.hpp>
2223
#include <cuspatial/error.hpp>
2324
#include <cuspatial/polygon_bounding_box.hpp>
2425
#include <cuspatial/polyline_bounding_box.hpp>
@@ -73,4 +74,21 @@ Napi::Value compute_polyline_bounding_boxes(CallbackArgs const& args) {
7374
return output;
7475
}
7576

77+
Napi::Value lonlat_to_cartesian(CallbackArgs const& args) {
78+
double origin_lon = args[0];
79+
double origin_lat = args[1];
80+
Column::wrapper_t lons_column = args[2];
81+
Column::wrapper_t lats_column = args[3];
82+
rmm::mr::device_memory_resource* mr = args[4];
83+
auto result = [&]() {
84+
try {
85+
return cuspatial::lonlat_to_cartesian(origin_lon, origin_lat, *lons_column, *lats_column, mr);
86+
} catch (std::exception const& e) { throw Napi::Error::New(args.Env(), e.what()); }
87+
}();
88+
auto output = Napi::Object::New(args.Env());
89+
output.Set("x", Column::New(args.Env(), std::move(result.first)));
90+
output.Set("y", Column::New(args.Env(), std::move(result.second)));
91+
return output;
92+
}
93+
7694
} // namespace nv

modules/cuspatial/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414

1515
export * from './geometry';
1616
export * from './quadtree';
17+
export * from './spatial';

modules/cuspatial/src/node_cuspatial.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,10 @@ export declare function findPolylineNearestToEachPoint<T extends FloatingPoint>(
7878
polylinePointsY: Column<T>,
7979
memoryResource
8080
?: MemoryResource): {table: Table, names: ['point_index', 'polyline_index', 'distance']};
81+
82+
export declare function lonLatToCartesian<T extends FloatingPoint>(
83+
origin_lon: number,
84+
origin_lat: number,
85+
lats: Column<T>,
86+
lons: Column<T>,
87+
memoryResource?: MemoryResource): {x: Column<T>, y: Column<T>};

modules/cuspatial/src/node_cuspatial/geometry.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,11 @@ Napi::Value compute_polygon_bounding_boxes(CallbackArgs const& args);
3434
*/
3535
Napi::Value compute_polyline_bounding_boxes(CallbackArgs const& args);
3636

37+
/**
38+
* @brief Convert lon/lat coordinate columns into cartesian coordinates.
39+
*
40+
* @param args CallbackArgs JavaScript arguments list.
41+
*/
42+
Napi::Value lonlat_to_cartesian(CallbackArgs const& args);
43+
3744
} // namespace nv

modules/cuspatial/src/spatial.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2022, NVIDIA CORPORATION.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import {
16+
Column,
17+
FloatingPoint,
18+
Series,
19+
} from '@rapidsai/cudf';
20+
import {makePoints} from '@rapidsai/cuspatial';
21+
import {MemoryResource} from '@rapidsai/rmm';
22+
23+
import {
24+
lonLatToCartesian,
25+
} from './addon';
26+
27+
export function convertLonLatToCartesian<T extends FloatingPoint>(centerX: number,
28+
centerY: number,
29+
lonPoints: Series<T>,
30+
latPoints: Series<T>,
31+
memoryResource?: MemoryResource) {
32+
const {x, y} = lonLatToCartesian(
33+
centerX, centerY, lonPoints._col as Column<T>, latPoints._col as Column<T>, memoryResource);
34+
return makePoints(Series.new(x), Series.new(y));
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2022, NVIDIA CORPORATION.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import '@rapidsai/cudf/test/jest-extensions';
16+
17+
import {setDefaultAllocator} from '@rapidsai/cuda';
18+
import {Series} from '@rapidsai/cudf';
19+
import {convertLonLatToCartesian} from '@rapidsai/cuspatial';
20+
import {DeviceBuffer} from '@rapidsai/rmm';
21+
22+
setDefaultAllocator((byteLength: number) => new DeviceBuffer(byteLength));
23+
24+
describe('Spatial', () => {
25+
test(`convertLonLatToCartesian`, () => {
26+
const seriesX = Series.new([-120.0, -120.0]);
27+
const seriesY = Series.new([48.0, 49.0]);
28+
const result = convertLonLatToCartesian(1.0, 1.0, seriesX, seriesY);
29+
30+
expect(result.getChild('y').toArray())
31+
.toMatchObject({'0': -5222.222222222223, '1': -5333.333333333334});
32+
expect(result.getChild('x').toArray())
33+
.toMatchObject({'0': 12233.92375289575, '1': 12184.804692381627});
34+
});
35+
});

0 commit comments

Comments
 (0)