redis geohash java and spatial4j calculate distance

The redis3.2 version provides the geohash function
, the principle of geohash, refer to the accuracy and principle of geohash

Find the distance between two coordinates
    Use the geoadd command to add the latitude and longitude of the two coordinates
    Use the geodist command to get the distance between the two coordinates

    The jedis code is as follows

    Parameter description: The first parameter geoKey in geoadd represents a collection of geographic locations, the second and third parameters are the longitude and latitude of a certain coordinate, and the third parameter represents the member uniquely corresponding to the coordinate.

    private JedisPool pool = new JedisPool("192.168.92.128",6380);
    
    @Test
    public void testJedisGeoDistance(){
        Jedis jedis = pool.getResource();
        double beijingLon = 116.20;
        double beijingLat = 39.56;

        double shanghaiLon = 120.51;
        double shanghaiLat = 30.40;
        String geoKey = UUID.randomUUID().toString();
        jedis.geoadd(geoKey,beijingLon,beijingLat,"beijing");
        jedis.geoadd(geoKey,shanghaiLon,shanghaiLat,"shanghai");
        double distance = jedis.geodist("geoHashDistance","beijing","shanghai", GeoUnit.KM);
        System.out.println(distance);

    }

    The spring-data-redis code is as follows

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Test
    public void testSpringDataRedisGeoDistance(){
        Point beijing = new Point(116.20,39.56);
        Point shanghai = new Point(120.51,30.40);
        String geoKey = UUID.randomUUID().toString();
        redisTemplate.opsForGeo().add(geoKey,beijing,"beijing");
        redisTemplate.opsForGeo().add(geoKey,shanghai,"shanghai");
        Distance distance = redisTemplate.opsForGeo().distance(geoKey, "beijing", "shanghai", Metrics.KILOMETERS);
        System.out.println(distance.getValue());
    }

Get the geohash code of one or more coordinates (redis is only accurate to the eleventh)

       

    @Test
    public void testGeoHash(){
        Jedis jedis = pool.getResource();
        double beijingLon = 116.20;
        double beijingLat = 39.56;
        String geoKey = UUID.randomUUID().toString();
        jedis.geoadd(geoKey,beijingLon,beijingLat,"beijing");
        String geoHash = jedis.geohash(geoKey,"beijing").get(0);
        //spring-data-redis 获取坐标的geohash编码
//        redisTemplate.opsForGeo().hash(geoKey,"beijing");
        System.out.println(geoHash);
    }

Get the coordinates near a point (using radius or radiusMember method)

    @Test
    public void testRadius(){
        Point beijing = new Point(116.20,39.56);
        Point shanghai = new Point(120.51,30.40);
        Point myPoint = new Point(112.36,29.70);
        String geoKey = UUID.randomUUID().toString();
        redisTemplate.opsForGeo().add(geoKey,beijing,"beijing");
        redisTemplate.opsForGeo().add(geoKey,shanghai,"shanghai");
        redisTemplate.opsForGeo().add(geoKey,myPoint,"mypoint");
        Distance distance = new Distance(1000,Metrics.KILOMETERS);
        RedisGeoCommands.GeoRadiusCommandArgs param = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending();
        GeoResults<RedisGeoCommands.GeoLocation<String>> results =  redisTemplate.opsForGeo().radius(geoKey,"mypoint",distance,param);
        results.getContent().forEach(new Consumer<GeoResult<RedisGeoCommands.GeoLocation<String>>>() {
            @Override
            public void accept(GeoResult<RedisGeoCommands.GeoLocation<String>> geoLocationGeoResult) {
                System.out.println(geoLocationGeoResult.getContent().getName() + " : " + geoLocationGeoResult.getDistance().getValue());
            }
        });
        //使用jedis
//        Jedis jedis = pool.getResource();
//        GeoRadiusParam param = GeoRadiusParam.geoRadiusParam().withHash().withDist().withCoord().sortAscending();
//        List<GeoRadiusResponse> list = jedis.georadiusByMember(geoKey,"mypoint",1000,GeoUnit.KM,param);
//        System.out.println(list);
    }

Use spatial4j to calculate the distance between two points

Import dependencies

<dependency>
      <groupId>org.locationtech.spatial4j</groupId>
      <artifactId>spatial4j</artifactId>
      <version>0.8</version>
    </dependency>
    private static SpatialContext geo = SpatialContext.GEO;

    @Test
    public void testSpatial4J(){
        org.locationtech.spatial4j.shape.Point point = new PointImpl(116.20,39.56,geo);
        org.locationtech.spatial4j.shape.Point point1 = new PointImpl(120.51,30.40,geo);
        double distance = geo.calcDistance(point,point1) * DistanceUtils.DEG_TO_KM;
        System.out.println(distance);
    }

Note that geo.calcDistance(point,point1) gets the degree between two points, the distance between two points = degree multiplied by DistanceUtils.DEG_TO_KM

Spatail4j obtains the geohash code of the coordinates (parameter 12 means that the geohash code is accurate to the 12th digit)

    @Test
    public void testSpatial4JGeoHash(){
        String geoHash = GeohashUtils.encodeLatLon(39.56,116.2,12);
        System.out.println(geoHash);
    }

 

Guess you like

Origin blog.csdn.net/name_is_wl/article/details/113572207