Common algorithms for business functions near o2o: geohash and spatial4j debugging actual combat

2019/12/0607:55:05 technology 319

开篇

 I have debugged a lot of open source projects, including open source mall, IM instant messaging, etc. If you are interested, you can check the source code and click on my avatar to enter. If you find it useful, please pay attention to it. 

Recently, I was looking at the principles of o2o, and there are a lot of principles of geohash algorithm on the Internet. But in fact, there are many principles on the Internet, but the real debugging is rare. Today, I will write the code to debug the geo algorithm and the spatial4 tool j. Easy to achieve, nearby businesses function . If you find it useful, just like 转个发吧. It’s best to follow along when you read the article. If you don’t have time, just bookmark it and forward it.


Common algorithms for business functions near o2o: geohash and spatial4j debugging actual combat - DayDayNews

geohash algorithm principle introduction

geo is the abbreviation of geography of geographic location, hash is actually hash algorithm. Everyone knows that we usually use Baidu map, and Gaode map actually has latitude and longitude for every point. You can understand it as geohash, which is actually the hash value of latitude and longitude after a series of calculations. The prefixes of geohash values ​​obtained by similar latitude and longitude are almost the same, so that all points in an area can be directly found in the database. Of course, the specific principle You can search by yourself. I won’t introduce it here. Today I will focus on how to use it.

The source code can be viewed on github: https://github.com/kungfoo/geohash-java


Common algorithms for business functions near o2o: geohash and spatial4j debugging actual combat - DayDayNews

spatial Tools

Actually everyone knows that the earth is spherical, but basically you can think of it as a plane on the map. For example, how do you check the nearby 1km range? Given the center coordinates and radius, find the coordinates of the four vertices of the square circumscribed by the circle. Isn't it? We use spatial4j for this calculation. The address on github is: https://github.com/locationtech/spatial4j


Common algorithms for business functions near o2o: geohash and spatial4j debugging actual combat - DayDayNews

Core debugging

1, build table, build Project

Create a new spring boot project, integrate mybatis, etc., of course, create a merchant table with merchant name, latitude and longitude, geohash and other fields.

CREATE TABLE &x60;merchant&x60; (
&x60;id&x60; INT(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT &39;Auto-incrementing primary key&39;,
&x60;merchant_name&x60; VARCHAR(64) name NOT NULL COMMENT &x60; VARCHAR(64) &39;,
&x60;longitude&x60; DOUBLE(9,6) NOT NULL COMMENT &39;longitude&39;,
&x60;latitude&x60; DOUBLE(8,6) NOT NULL COMMENT &39;latitude&39;,
&x60;geo_code&x60 ; CHAR(12) NOT NULL COMMENT &39;geohash code&39;,
PRIMARY KEY (&x60;id&x60;),
KEY &x60;idx_merchant_longitude_latitude&x60; (&x60;longitude&x60;,&x60;latitude&x60;),
KEY &x60;idx_merchant_geo_code&x60; (&x60;geo_code&x60;)
)COMMENT=&39;Merchant Table&=f8a4bSETa#ut7#

Common algorithms for business functions near o2o: geohash and spatial4j debugging actual combat - DayDayNews

2, method one, query the nearby 1km with spatial4j to achieve

A, first introduce the jar package:

 
com.spatial4j
spatial4j
0.5


Common algorithms for business functions near o2o: geohash and spatial4j debugging actual combat - DayDayNewsa7b#da0a04, four points The value of

public static void main(String[] args) {
//Starting point latitude and longitude
double lon = 116.312528, lat = 39.983733;
// kilometers
int radius = 1;
SpatialContext geo = SpatialContext.GEO;
Rectangle rectangle = geo.getDistCalc().calcBoxByDistFromPt(
geo.makePoint(lon, lat),
radius * DistanceUtils.KM_TO_DEG, geo, null);

System .out.println(rectangle.getMinX() +
"-" + rectangle.getMaxX());// Longitude range
System.out.println(rectangle.getMinY() +
"-" + rectangle .getMaxY());// Latitude range
}


Common algorithms for business functions near o2o: geohash and spatial4j debugging actual combat - DayDayNews

C. According to the range, search in the database and find the nearby 1km data. Of course, the latitude and longitude are best indexed together. These two data are calculated above.

SELECT id, merchant_name
FROM merchant
WHERE (longitude BETWEEN? AND ?) AND (latitude BETWEEN? AND ?);

3, according to geohash query. The geo_code field will be used at this time.

A. Introduce the jar package of geohash



ch.hsr
c8478bf0b0192b#c8478b0#0192#


Common algorithms for business functions near o2o: geohash and spatial4j debugging actual combat - DayDayNews

B. Compare with the length corresponding range table of geohash, we can know that to query the nearby 1km data, as long as the length is 5. For specific calculations, GeohashUtils.encodeLatLon(lat, lon, 5) can be substituted into the bottom line sql.

SELECT id, merchant_name
FROM merchant
WHERE geo_code LIKE CONCAT(?, &39;%&39;);

Common algorithms for business functions near o2o: geohash and spatial4j debugging actual combat - DayDayNews

C, but geohash has boundary problems . Because geohash is a common hash in a certain area, points that are very close outside the index boundary may cause geohash to be completely different, so what should I do? In fact, it is not difficult to solve, you can calculate the geohash of the eight surrounding areas at this time.

public static void main(String[] args) {
// mobile device latitude and longitude
double lon = 116.312528, lat = 39.983733;
GeoHash geoHash = GeoHash.
withCharacterPrecision8, lat , 6);
// current
System.out.println(geoHash.toBase32());
// N, NE, E, SE, S, SW, W, NW
System.out.println( "---------------------------");
//Southeast, Northwest, Northeast, Northwest, Southeast, Southwest, etc.
GeoHash[ ] adjacent = geoHash.getAdjacent();
for (GeoHash hash: adjacent) {
System.out.println(hash.toBase32());
}
}

Common algorithms for business functions near o2o: geohash and spatial4j debugging actual combat - DayDayNews


Common algorithms for business functions near o2o: geohash and spatial4j debugging actual combat - DayDayNews

D, mysql query, query the merchants in these areas to , geohash boundary problem can be solved.

SELECT id, merchant_name
FROM merchant
WHERE geo_code IN (?, ?, ?, ?, ?, ?, ?, ?, ?);

The most common in

o2o The application scenario of is a nearby business. I hope this article will help o2o students. If you find it useful, just like and forward it.

In addition, I also debugged many other open source projects

Java open source based on microservice Spring cloud rapid development scaffolding debugging actual combat

Super easy to use Java open source verification code Artifact

Java built open source spring boot mall system actual combat

The open source of the front and back ends is separatedOnline test system debugging actual combat

Debug an open source Java lightweight high-performance IM, single machine supports hundreds of thousands to millions of online users

Java 100% open source CMS System project debugging actual combat

The open source CMS system debugging actual combat written by the front-end cattle, the fluid layout is compatible with the mobile phone browser

If you think it is useful, you can pay attention to it

technology Category Latest News