Skip to content

Commit

Permalink
Added gdal.Geometry.fromGeoJson() (backported from yocontra/node-gdal…
Browse files Browse the repository at this point in the history
…-next@792e2aa, with the addition of string handling) [skip ci]
  • Loading branch information
brianreavis committed Sep 25, 2020
1 parent ee1ffc0 commit 6db3d63
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

Read and write raster and vector geospatial datasets straight from [Node.js](http://nodejs.org) with this native [GDAL](http://www.gdal.org/) binding. GDAL [2.0.1](http://trac.osgeo.org/gdal/wiki/Release/2.0.1-News) ([GEOS](http://trac.osgeo.org/geos/) [3.4.2](http://trac.osgeo.org/geos/browser/tags/3.4.2/NEWS), [Proj.4](http://trac.osgeo.org/proj/) [4.8.0](http://www.osgeo.org/node/1268)) comes bundled, so node-gdal will work straight out of the box. To get started, browse the [**API Documentation**](http://naturalatlas.github.io/node-gdal/classes/gdal.html) or [examples](examples/).

For an alternate fork that's a little more bleeding-edge, check out [gdal-next](https://www.npmjs.com/package/gdal-next). The [module-alias](https://www.npmjs.com/package/module-alias) package can be useful if gdal is deep in your dependency tree and you don't want to update references everywhere.

```sh
$ npm install gdal --save
```
Expand Down Expand Up @@ -62,9 +60,13 @@ $ make test # test against bundled gdal
$ make test-shared # test against shared gdal
```

## Alternate Fork

For an alternate fork that's more bleeding-edge at the moment, check out [gdal-next](https://www.npmjs.com/package/gdal-next) and evaluate which project works best for your needs. The [module-alias](https://www.npmjs.com/package/module-alias) package can be useful if gdal is deep in your dependency tree and you don't want to update references everywhere.

## License

Copyright © 2015–2017 [Natural Atlas, Inc.](https://github.com/naturalatlas) & [Contributors](https://github.com/naturalatlas/node-gdal/graphs/contributors)
Copyright © 2015–2020 [Natural Atlas, Inc.](https://github.com/naturalatlas) & [Contributors](https://github.com/naturalatlas/node-gdal/graphs/contributors)

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0

Expand Down
43 changes: 43 additions & 0 deletions src/gdal_geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void Geometry::Initialize(Local<Object> target)
//Nan::SetMethod(constructor, "fromWKBType", Geometry::create);
Nan::SetMethod(lcons, "fromWKT", Geometry::createFromWkt);
Nan::SetMethod(lcons, "fromWKB", Geometry::createFromWkb);
Nan::SetMethod(lcons, "fromGeoJson", Geometry::createFromGeoJson);
Nan::SetMethod(lcons, "getName", Geometry::getName);
Nan::SetMethod(lcons, "getConstructor", Geometry::getConstructor);

Expand Down Expand Up @@ -888,6 +889,48 @@ NAN_METHOD(Geometry::createFromWkb)
info.GetReturnValue().Set(Geometry::New(geom, true));
}


/**
* Creates a Geometry from a GeoJSON string. Requires GDAL>=2.3.
*
* @static
* @method fromGeoJson
* @param {Object} geojson
* @return gdal.Geometry
*/
NAN_METHOD(Geometry::createFromGeoJson) {
Nan::HandleScope scope;
#if GDAL_VERSION_MAJOR < 2 || (GDAL_VERSION_MAJOR <= 2 && GDAL_VERSION_MINOR < 3)
Nan::ThrowError("GDAL < 2.3 does not support parsing GeoJSON directly");
return;
#else
Local<Value> input;
NODE_ARG_OBJECT(0, "geojson", input);

std::string val;
if (input->IsString()) {
val = *Nan::Utf8String(input);
} else if (input->IsObject() && !input->IsNull()) {
// goes to text to pass it in, there isn't a performant way to
// go from v8 JSON -> CPLJSON anyways
Nan::JSON NanJSON;
Nan::MaybeLocal<String> result = NanJSON.Stringify(geo_obj);
if (result.IsEmpty()) {
Nan::ThrowError("Invalid GeoJSON");
return;
}
Local<String> stringified = result.ToLocalChecked();
val = *Nan::Utf8String(stringified);
} else {
Nan::ThrowError("Invalid GeoJSON (must a GeoJSON object or serialized string)");
return;
}

OGRGeometry *geom = OGRGeometryFactory::createFromGeoJson(val.c_str());
info.GetReturnValue().Set(Geometry::New(geom, true));
#endif
}

/**
* Creates an empty Geometry from a WKB type.
*
Expand Down
1 change: 1 addition & 0 deletions src/gdal_geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Geometry: public Nan::ObjectWrap {
static NAN_METHOD(create);
static NAN_METHOD(createFromWkt);
static NAN_METHOD(createFromWkb);
static NAN_METHOD(createFromGeoJson);
static NAN_METHOD(getName);
static NAN_METHOD(getConstructor);

Expand Down
2 changes: 1 addition & 1 deletion test/api_algorithms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('gdal', function() {

assert(lyr.features.count() > 0, 'features were created');

if (process.env.TARGET !== 'shared') {
if (process.env.TARGET !== 'SHARED') {
// for some reason shared gdal on travis has different contour behavior
// https://travis-ci.org/github/naturalatlas/node-gdal/builds/730319832
// (not really the binding's fault)
Expand Down
20 changes: 18 additions & 2 deletions test/api_geometry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('gdal.Geometry', function() {
});
describe('toWKB()', function() {
it('should return valid result', function() {
var point2d = new gdal.Point(1,2);
var point2d = new gdal.Point(1, 2);
var wkb = point2d.toWKB();
var expected;
if (wkb[0] === 0) {
Expand All @@ -72,7 +72,7 @@ describe('gdal.Geometry', function() {
});
describe('toWKT()', function() {
it('should return valid result', function() {
var point2d = new gdal.Point(1,2);
var point2d = new gdal.Point(1, 2);
var wkt = point2d.toWKT();
assert.equal(wkt, 'POINT (1 2)');
});
Expand All @@ -94,6 +94,22 @@ describe('gdal.Geometry', function() {
assert.equal(point2d.y, 2);
});
});
if (parseFloat(gdal.version) >= 2.3) {
describe('fromGeoJson()', function() {
it('should return valid result from object', function() {
var point2d = gdal.Geometry.fromGeoJson({ type: 'Point', coordinates: [ 2, 1 ] });
assert.equal(point2d.wkbType, gdal.wkbPoint);
assert.equal(point2d.x, 2);
assert.equal(point2d.y, 1);
});
it('should return valid result from string', function() {
var point2d = gdal.Geometry.fromGeoJson(JSON.stringify({ type: 'Point', coordinates: [ 2, 1 ] }));
assert.equal(point2d.wkbType, gdal.wkbPoint);
assert.equal(point2d.x, 2);
assert.equal(point2d.y, 1);
});
});
}
describe('getConstructor()', function() {
// wkbUnknown = 0, wkbPoint = 1, wkbLineString = 2, wkbPolygon = 3,
// wkbMultiPoint = 4, wkbMultiLineString = 5, wkbMultiPolygon = 6, wkbGeometryCollection = 7,
Expand Down

0 comments on commit 6db3d63

Please sign in to comment.