Skip to content

Commit

Permalink
adding docstrings and dartfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
anovis committed Oct 27, 2020
1 parent a6187e6 commit 95e6fce
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 65 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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]

Expand Down
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -7,7 +8,7 @@ For example the point

`25.6953° S, 54.4367° W`

will covert to
will covert to

`6g3mc626`.

Expand All @@ -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<String> proximityGeohashes = createGeohashes(48.864716, 2.349014, 5000, 3);
List<String> 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)
126 changes: 75 additions & 51 deletions lib/proximity_hash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,83 +3,107 @@ import 'package:dart_geohash/dart_geohash.dart';

GeoHasher geoHasher = GeoHasher();

List<double> getCentroid(latitude, longitude, height, width){
/// Get centroid
List<double> 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<String> 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<String> createGeohashes(
double latitude, double longitude, double radius, int precision) {
if (precision > 12 || precision < 0) {
throw ArgumentError('Incorrect precision found');
}

double x = 0.0;
double y = 0.0;

List<String> geohashes = [];

List<double> 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<double> 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<double> 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<double> 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<double> 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<double> 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;

}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
7 changes: 3 additions & 4 deletions test/proximity_hash_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,23 @@ void main() {
});
group('Centroid tests', () {
test('get centroid', () {
List<double> centroid = getCentroid(10,10,10,10);
List<double> 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<String> geohashes = createGeohashes(48.858156, 2.294776, 100, 3);
assert(geohashes.length==2);
assert(geohashes.length == 2);
assert(geohashes.contains("u0d"));
assert(geohashes.contains("u09"));
});
});

}

0 comments on commit 95e6fce

Please sign in to comment.