Skip to content
Permalink
Browse files

feature: Circle#isInside Polygon#center

  • Loading branch information...
MikeMitterer committed Jan 26, 2016
1 parent 5a03f44 commit cce0a4da93f03977a4545c6470c85b5d5243637e
@@ -4,19 +4,16 @@
```dart
final Distance distance = new Distance();
expect(
distance.vincenty(new LatLng(52.518611,13.408056),new LatLng(51.519475,7.46694444)),
422592.0);
expect(
distance(new LatLng(52.518611,13.408056),new LatLng(51.519475,7.46694444)),
422592.0);
expect(distance.as(LengthUnit.Kilometer,
new LatLng(52.518611,13.408056),new LatLng(51.519475,7.46694444)),
423.0);
// km = 423
final int km = distance.as(LengthUnit.Kilometer,
new LatLng(52.518611,13.408056),new LatLng(51.519475,7.46694444));
// meter = 422592
final int meter = distance(new LatLng(52.518611,13.408056),new LatLng(51.519475,7.46694444));
```

![LatLong](http://eogn.com/images/newsletter/2014/Latitude-and-longitude.png)

## Features and bugs
Please file feature requests and bugs at the [issue tracker](https://github.com/MikeMitterer/dart-latlong/issues).

@@ -17,7 +17,25 @@
* limitations under the License.
*/

/// This is an awesome library. More dartdocs go here.
/// Helps with latitude / longitude calculations.
///
/// For distance calculations the default algorithm is [distanceWithVincenty]
///
/// final Distance distance = new Distance();
///
/// // km = 423
/// final int km = distance.as(LengthUnit.Kilometer,
/// new LatLng(52.518611,13.408056),new LatLng(51.519475,7.46694444));
///
/// // meter = 422592
/// final int meter = distance(new LatLng(52.518611,13.408056),new LatLng(51.519475,7.46694444));
///
/// Find more infos on [Movable Type Scripts](http://www.movable-type.co.uk/scripts/latlong.html)
///
/// ![LatLong](http://eogn.com/images/newsletter/2014/Latitude-and-longitude.png)
///
/// ![Map](http://www.isobudgets.com/wp-content/uploads/2014/03/latitude-longitude.jpg)
///
library latlong;

import 'dart:math' as math;
@@ -28,5 +46,48 @@ part "latlong/LatLng.dart";
part "latlong/Distance.dart";
part "latlong/LengthUnit.dart";

part "latlong/Polygon.dart";
part "latlong/Circle.dart";

/// Earth radius in meter
const double EARTH_RADIUS = 6378137.0;

/// Converts degree to radian
double degToRadian(final double deg) => deg * (math.PI / 180.0);

/// Radian to degree
double radianToDeg(final double rad) => rad * (180.0 / math.PI);

/// Rounds [value] to given number of [decimals]
double round(final double value, { final int decimals: 6 })
=> (value * math.pow(10,decimals)).round() / math.pow(10,decimals);

/// Convert a bearing to be within the 0 to +360 degrees range.
/// Compass bearing is in the rangen 0° ... 360°
double normalizeBearing(final double bearing) => (bearing + 360) % 360;

/// Converts a decimal coordinate value to sexagesimal format
///
/// final String sexa1 = decimal2sexagesimal(51.519475);
/// expect(sexa1, '51° 31\' 10.11"');
///
String decimal2sexagesimal(final double dec) {
List<int> _split(final double value) {
final List<String> tmp = value.toString().split('.');
return <int>[ int.parse(tmp[0]).abs(), int.parse(tmp[1])];
}

final List<int> parts = _split(dec);
final int integerPart = parts[0];
final int fractionalPart = parts[1];

final int deg = integerPart;
final double min = double.parse("0.${fractionalPart}") * 60;

final List<int> minParts = _split(min);
final int minFractionalPart = minParts[1];

final double sec = (double.parse("0.${minFractionalPart}") * 60);

return "${deg}° ${min.floor()}' ${sec.toStringAsFixed(2)}\"";
}
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2016, Michael Mitterer (office@mikemitterer.at),
* IT-Consulting and Development Limited.
*
* All Rights Reserved.
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

part of latlong;


/// Circle-base GEO algorithms.
///
/// Circle use by default the Haversine-Algorithm for distance computations
class Circle {
// final Logger _logger = new Logger('latlong.Circle');

final double radius;
final LatLng center;
final DistanceAlgorithm _algorithm;

Circle(final LatLng this.center, this.radius, { final DistanceAlgorithm algorithm: distanceWithHaversine })
: _algorithm = algorithm;

/// Checks if a [point] is inside the given [Circle]
///
/// final Circle circle = new Circle(new LatLng(0.0,0.0), 111319.0);
/// final LatLng newPos = new LatLng(1.0,0.0);
///
/// expect(circle.isPointInside(newPos),isTrue);
///
/// final Circle circle2 = new Circle(new LatLng(0.0,0.0), 111318.0);
///
/// expect(circle2.isPointInside(newPos),isFalse);
///
bool isPointInside(final LatLng point) {
Validate.notNull(point);

final Distance distance = new Distance(algorithm: _algorithm);

final double dist = distance(center, point);
return dist <= radius;
}

//- private -----------------------------------------------------------------------------------
}
Oops, something went wrong.

0 comments on commit cce0a4d

Please sign in to comment.
You can’t perform that action at this time.