Skip to content

Commit

Permalink
Merge pull request #12 from maRci002/feat/nadgrids
Browse files Browse the repository at this point in the history
add nadgrids
  • Loading branch information
maRci002 authored Aug 20, 2022
2 parents 7002ece + cc6eb0c commit 7c95696
Show file tree
Hide file tree
Showing 15 changed files with 731 additions and 152 deletions.
42 changes: 26 additions & 16 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
{
"version": "0.2.0",
"configurations": [{
"name": "Dart-proj4",
"program": "example/proj4dart_example.dart",
"request": "launch",
"type": "dart"
}, {
"name": "Dart-ogc-wkt",
"program": "example/proj4dart_ogc_wkt_example.dart",
"request": "launch",
"type": "dart"
}, {
"name": "Dart-esri-wkt",
"program": "example/proj4dart_esri_wkt_example.dart",
"request": "launch",
"type": "dart"
}]
"configurations": [
{
"name": "Dart-proj4",
"program": "example/proj4dart_example.dart",
"request": "launch",
"type": "dart"
},
{
"name": "Dart-ogc-wkt",
"program": "example/proj4dart_ogc_wkt_example.dart",
"request": "launch",
"type": "dart"
},
{
"name": "Dart-esri-wkt",
"program": "example/proj4dart_esri_wkt_example.dart",
"request": "launch",
"type": "dart"
},
{
"name": "Dart-shift-grid",
"program": "example/proj4dart_shift_grid_example.dart",
"request": "launch",
"type": "dart"
}
]
}
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,43 @@ var projection = Projection.parse(def);

For full example visit [example/proj4dart_esri_wkt_example.dart](example/proj4dart_esri_wkt_example.dart)

#### Grid Based Datum Adjustments

To use `+nadgrids=` in a proj definition, first read your NTv2 `.gsb` file into an `Uint8List`, then pass to `Projection.nadgrid`. E.g:

Dart:
```dart
final bytes = await File(
'assets/my_grid.gsb',
).readAsBytes();
Projection.nadgrid('key', bytes);
```

Flutter:
```dart
import 'package:flutter/services.dart' show rootBundle;
final bytes = (await rootBundle.load(fileName)).buffer.asUint8List();
Projection.nadgrid('key', bytes);
```

then use the given key in your definition, e.g. `+nadgrids=@key,null`. See [Proj4 General Parameters](http://proj.maptools.org/gen_parms.html).

Nadgrid example:

```dart
import 'package:flutter/services.dart' show rootBundle;
final bytes = (await rootBundle.load('assets/nzgd2kgrid0005.gsb')).buffer.asUint8List();
Projection.nadgrid('nzgd2kgrid0005.gsb', bytes);
var def = '+proj=longlat +datum=nzgd49 +towgs84=59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993 +nadgrids=nzgd2kgrid0005.gsb +no_defs';
var namedProjection = Projection.add('EPSG:4272', def);
```

For full example visit [example/proj4dart_shift_grid_example.dart](example/proj4dart_shift_grid_example.dart)

### Transform between Projections

```dart
Expand Down
41 changes: 41 additions & 0 deletions example/proj4dart_shift_grid_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'dart:io';

import 'package:proj4dart/proj4dart.dart';

void main() async {
// read bytes from filesystem
// In case of Flutter assets:
// import 'package:flutter/services.dart' show rootBundle;
// final bytes = (await rootBundle.load(fileName)).buffer.asUint8List();
final bytes = await File(
'test/test_resources/ntv2_0_downsampled.gsb',
).readAsBytes();

// load nadgrid
Projection.nadgrid('ntv2', bytes);

// Define Point
var pointSrc = Point(x: -44.382211538462, y: 40.3768);

// Define some projection which has '+nadgrids='
var projSrc = Projection.add(
'ntv2_from',
'+proj=longlat +ellps=clrk66 +nadgrids=@ignorable,ntv2,null',
);
;

// Use built-in projection
var projDst = Projection.WGS84;

// Forward transform (projected crs -> lonlat)
var pointForward = projSrc.transform(projDst, pointSrc);
print(
'FORWARD: Transform point ${pointSrc.toArray()} from ntv2_from to WGS84: ${pointForward.toArray()}');
// FORWARD: Transform point [-44.382211538462, 40.3768] from ntv2_from to WGS84: [-44.38074905319326, 40.37745745991217]

// Inverse transform (lonlat -> projected crs)
var pointInverse = projDst.transform(projSrc, pointForward);
print(
'INVERSE: Transform point ${pointForward.toArray()} from WGS84 to ntv2_from: ${pointInverse.toArray()}');
// INVERSE: Transform point [-44.38074905319326, 40.37745745991217] from WGS84 to ntv2_from: [-44.38074905319326, 40.37745745991217]
}
11 changes: 9 additions & 2 deletions lib/src/classes/datum.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:proj4dart/src/classes/nadgrid.dart';
import 'package:proj4dart/src/constants/values.dart' as consts;

class Datum {
Expand All @@ -7,13 +8,15 @@ class Datum {
final double b;
final double es;
final double ep2;
final List<NadgridParam>? grids;

Datum(String? datumCode, List<double>? datum_params, double a, double b,
double es, double ep2)
double es, double ep2, List<NadgridParam>? nadgrids)
: a = a,
b = b,
es = es,
ep2 = ep2 {
ep2 = ep2,
grids = nadgrids {
if (datumCode == null || datumCode == 'none') {
datumType = consts.PJD_NODATUM;
} else {
Expand All @@ -37,5 +40,9 @@ class Datum {
}
}
}

if (nadgrids != null) {
datumType = consts.PJD_GRIDSHIFT;
}
}
}
Loading

0 comments on commit 7c95696

Please sign in to comment.