diff --git a/CHANGELOG.md b/CHANGELOG.md index ebdf4d1..bb06843 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ +## [1.0.2] + +* Add docstrings and fixed formating according to dartfmt + ## [1.0.1] -* Removed dependency on flutter +* Removed dependency on flutter ## [1.0.0] diff --git a/README.md b/README.md index dba7736..3672391 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # Dart Proximity Hash + ![Pub Version](https://img.shields.io/pub/v/proximity_hash) **Proximity Hash** generates a set of geohashes that cover a circular area, given the center coordinates and the radius. Geohash is a public domain geocode system which encodes a geographic location into a short string of letters and digits and is used as unique identifies and to represent point data in databases. @@ -7,7 +8,7 @@ For example the point `25.6953° S, 54.4367° W` -will covert to +will covert to `6g3mc626`. @@ -17,29 +18,30 @@ To get this plugin, add `proximity_hash` as a [dependency in your pubspec.yaml f ```yaml dependencies: - proximity_hash: ^1.0.0 + proximity_hash: ^1.0.2 ``` ## Usage -To get all the geohashes within a radius of `5000` meters from the point `25.6953° S, 54.4367` with precision `3` simply import the library and call `createGeohashes()`. The precision dictates how long and how many geohashes are returned. +To get all the geohashes within a radius of `5000` meters from the point `25.6953° S, 54.4367° W` with precision `3` simply import the library and call `createGeohashes()`. The precision dictates how long and how many geohashes are returned. For +example a precision of 3 returns geohahses of length 3. ``` dart import 'package:proximity_hash/proximity_hash.dart'; -List proximityGeohashes = createGeohashes(48.864716, 2.349014, 5000, 3); +List proximityGeohashes = createGeohashes(48.864716, 2.349014, 5000, 3); + +// proximityGeohashes == ["u0d","u09"] ``` ## Issues -Please file any issues, bugs or feature requests as an issue on our [GitHub](https://github.com/anovis/proximity_hash/issues) page. +Please file any issues, bugs or feature requests as an issue on our [GitHub](https://github.com/anovis/proximity_hash/issues) page. ## Want to contribute If you would like to contribute to the plugin (e.g. by improving the documentation, solving a bug or adding a cool new feature) submit a [pull request](https://github.com/anovis/proximity_hash/pulls). - ___ -Based on https://github.com/ashwin711/proximityhash - +Based on [proximityhash](https://github.com/ashwin711/proximityhash) diff --git a/lib/proximity_hash.dart b/lib/proximity_hash.dart index 2665f60..808def0 100644 --- a/lib/proximity_hash.dart +++ b/lib/proximity_hash.dart @@ -3,46 +3,47 @@ import 'package:dart_geohash/dart_geohash.dart'; GeoHasher geoHasher = GeoHasher(); -List getCentroid(latitude, longitude, height, width){ +/// Get centroid +List getCentroid(latitude, longitude, height, width) { + double centeredY = latitude + (height / 2); + double centeredX = longitude + (width / 2); - double centeredY = latitude + (height / 2); - double centeredX = longitude + (width / 2); - - return [centeredX, centeredY]; + return [centeredX, centeredY]; } -bool inCircleCheck(double latitude, double longitude, double centerLat, double centerLon, double radius){ +/// Check to see if a point is contained in a circle +bool inCircleCheck(double latitude, double longitude, double centerLat, + double centerLon, double radius) { + double xDiff = longitude - centerLon; + double yDiff = latitude - centerLat; - double xDiff = longitude - centerLon; - double yDiff = latitude - centerLat; - - if (pow(xDiff, 2) + pow(yDiff, 2) <= pow(radius, 2)){ - return true; - } + if (pow(xDiff, 2) + pow(yDiff, 2) <= pow(radius, 2)) { + return true; + } - return false; + return false; } -String convertToGeohash(y, x, latitude, longitude, int precision){ +/// Convert location point to geohash taking into account Earth curvature +String convertToGeohash(y, x, latitude, longitude, int precision) { + double pi = 3.14159265359; - double pi = 3.14159265359; + double rEarth = 6371000; - double rEarth = 6371000; + double latDiff = (y / rEarth) * (180 / pi); + double lonDiff = (x / rEarth) * (180 / pi) / cos(latitude * pi / 180); - double latDiff = (y / rEarth) * (180 / pi); - double lonDiff = (x / rEarth) * (180 / pi) / cos(latitude * pi/180); + double finalLat = latitude + latDiff; + double finalLon = longitude + lonDiff; - double finalLat = latitude+latDiff; - double finalLon = longitude+lonDiff; - - return geoHasher.encode(finalLon, finalLat,precision:precision); + return geoHasher.encode(finalLon, finalLat, precision: precision); } -// radius in meters -List createGeohashes(double latitude,double longitude, double radius, int precision){ - - if (precision > 12 || precision < 0){ - throw ArgumentError('Incorrect precision found'); +// Generate geohashes based on radius in meters +List createGeohashes( + double latitude, double longitude, double radius, int precision) { + if (precision > 12 || precision < 0) { + throw ArgumentError('Incorrect precision found'); } double x = 0.0; @@ -50,36 +51,59 @@ List createGeohashes(double latitude,double longitude, double radius, in List geohashes = []; - List gridWidth = [5009400.0, 1252300.0, 156500.0, 39100.0, 4900.0, 1200.0, 152.9, 38.2, 4.8, 1.2, 0.149, 0.0370]; - List gridHeight = [4992600.0, 624100.0, 156000.0, 19500.0, 4900.0, 609.4, 152.4, 19.0, 4.8, 0.595, 0.149, 0.0199]; - - double height = (gridHeight[precision - 1])/2; - double width = (gridWidth[precision-1])/2; + List gridWidth = [ + 5009400.0, + 1252300.0, + 156500.0, + 39100.0, + 4900.0, + 1200.0, + 152.9, + 38.2, + 4.8, + 1.2, + 0.149, + 0.0370 + ]; + List gridHeight = [ + 4992600.0, + 624100.0, + 156000.0, + 19500.0, + 4900.0, + 609.4, + 152.4, + 19.0, + 4.8, + 0.595, + 0.149, + 0.0199 + ]; + + double height = (gridHeight[precision - 1]) / 2; + double width = (gridWidth[precision - 1]) / 2; int latMoves = (radius / height).ceil(); int lonMoves = (radius / width).ceil(); for (var i = 0; i < latMoves; i++) { - double tempLat = y + height*i; - for (var j = 0; j < lonMoves; j++) { - double tempLong = y + width*j; - - if (inCircleCheck(tempLat, tempLong, y, x, radius)){ - - List centerList = getCentroid(tempLat, tempLong, height, width); - double centerX = centerList[0]; - double centerY = centerList[1]; - - geohashes.addAll([ - convertToGeohash(centerY, centerX, latitude, longitude, precision), - convertToGeohash(-centerY, centerX, latitude, longitude, precision), - convertToGeohash(centerY, -centerX, latitude, longitude, precision), - convertToGeohash(-centerY, -centerX, latitude, longitude, precision), - ].toSet().toList()); - - } + double tempLat = y + height * i; + for (var j = 0; j < lonMoves; j++) { + double tempLong = y + width * j; + + if (inCircleCheck(tempLat, tempLong, y, x, radius)) { + List centerList = getCentroid(tempLat, tempLong, height, width); + double centerX = centerList[0]; + double centerY = centerList[1]; + + geohashes.addAll([ + convertToGeohash(centerY, centerX, latitude, longitude, precision), + convertToGeohash(-centerY, centerX, latitude, longitude, precision), + convertToGeohash(centerY, -centerX, latitude, longitude, precision), + convertToGeohash(-centerY, -centerX, latitude, longitude, precision), + ].toSet().toList()); + } } } return geohashes; - } diff --git a/pubspec.yaml b/pubspec.yaml index 611af39..94c9262 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: proximity_hash description: Generate a set of geohashes that cover a circular area, given the center coordinates and the radius -version: 1.0.1 +version: 1.0.2 homepage: https://github.com/anovis/proximity_hash environment: diff --git a/test/proximity_hash_test.dart b/test/proximity_hash_test.dart index 83e0056..397b19a 100644 --- a/test/proximity_hash_test.dart +++ b/test/proximity_hash_test.dart @@ -14,24 +14,23 @@ void main() { }); group('Centroid tests', () { test('get centroid', () { - List centroid = getCentroid(10,10,10,10); + List centroid = getCentroid(10, 10, 10, 10); assert(centroid[0] == 15.0); assert(centroid[1] == 15.0); }); }); group('Lat Lng Conversion tests', () { test('convert to geohash', () { - String geohash = convertToGeohash(1000.0,1000.0, 12.0, 77.0,10); + String geohash = convertToGeohash(1000.0, 1000.0, 12.0, 77.0, 10); assert(geohash == "tdnu26hmkq"); }); }); group('create geohash tests', () { test('create geohash', () { List geohashes = createGeohashes(48.858156, 2.294776, 100, 3); - assert(geohashes.length==2); + assert(geohashes.length == 2); assert(geohashes.contains("u0d")); assert(geohashes.contains("u09")); }); }); - }