Main codes are here: redis-geo and jedis-geo
The are two new features:
- add support to polygons
- add a command to check whether a point is inside a polygon
Three new commands are: geoaddpolygon, geogetpolygon and geopointinpolygon
usage
geoaddpolygon key member lng0 lat0 lng1 lat1 lng2 lat2 [... lngn latn] lng0 lat0
It is used to add a polygon. Remember that the first point must be same as the last point because it is a polygon
usage
geogetpolygon key memeber
It is used to get a polygon
usage
geopointinpolygon keyPoint memberPoint keyPolygon memberPolygon
It is used to check if the point is inside the polygon
It is based on redis but not merged into master branch. Source code can be see here. We just add nearly 200 lines (see here) to the origin codes.
Download redis-geo(only for linux) at
- google drive
or - baidu Pan. The password is 4pfu
If you want to use redis-geo on window or mac, please clone the source code and compile it.
We add three new functions to the class Jedis:
- Long geoaddpolygon(String key, String member, GeoPolygon polygon)
- GeoPolygon geogetpolygon(String key, String member)
- Boolean geopointinpolygon(String keyPoint, String memberPoint, String keyPolygon, String memberPolygon)
They correspond to the three new commands of redis-geo.
Download jedis-3.4.0-SNAPSHOT.jar at
- google drive
or - baidu Pan. The password is dkv5
It is based on redis but not merged into master branch. Source code can be see here. We just add nearly 170 lines (see here) to the origin codes.
- download redis-geo and run
bin/redis-server redis.conf
The default redis.conf set requirepass = foobared. If you don't want to set password, please delete the line.
- download the jedis-3.4.0-SNAPSHOT.jar and add it to external libraries, then test it with below codes
public static void main(String[] args) {
Jedis j = new Jedis("127.0.0.1", 6379);
j.auth("foobared");
List<GeoCoordinate> points = new ArrayList<>();
points.add(new GeoCoordinate(120, 30));
points.add(new GeoCoordinate(121, 31));
points.add(new GeoCoordinate(122, 30));
points.add(new GeoCoordinate(120, 30));
GeoPolygon polygon = new GeoPolygon(points);
long polygonStatus = j.geoaddpolygon("gp", "p0", polygon); // add polygon
long pointStatus = j.geoadd("pt", 121, 30.5, "pt0"); // add point
long pointStatus1 = j.geoadd("pt", 121, 34, "pt1"); // add point
Boolean res = j.geopointinpolygon("pt", "pt0", "gp", "p0"); // check if pto is in the polygon
Boolean res1 = j.geopointinpolygon("pt", "pt1", "gp", "p0");
System.out.println("pt0 in polygon: " + res);
System.out.println("pt1 in polygon: " + res1);
}
The output is
pt0 in polygon: true
pt1 in polygon: false