Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Solve the cross-region problem of geohash coding

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

In the previous article, we proposed to use geohash to solve the performance problem of matching points. the drawback of this algorithm is that if the point near the point is on the boundary, the point near the point may cross the border, that is, the corresponding point cannot be found through the geohash value of the point. For example:

The blue dots in the diagram are the points that need to be bound and mapped.

The red dot below is the point after the binding calculation.

The reason for the offset is

The point data on Suizhou street belongs to the partition above the blue dot when geohashcode is partitioned.

The results of the query in the database show that the road network data with blue dots in a partition is located in the following figure

So the data found are located on the road below, which leads to the offset problem.

In order to solve this problem, it is proposed to spread the hashcode code to find the nearby geohashcode code.

The code is as follows

So the data found are located on the road below, which leads to the offset problem.

In order to solve this problem, it is proposed to spread the hashcode code to find the nearby geohashcode code.

The code is as follows

"" A simple GeoHash implementation. "" `# Forward and reverse base 32 mapBASESEQUENCE = '0123456789bcdefghjkmnpqrstuvwxyz'BASE32MAP = dict ((k, count) for count, k in enumerate (BASESEQUENCE)) BASE32MAPR = dict ((count, k) for count, k in enumerate (BASESEQUENCE)) def _ bits_to_float (bits, lower=-90.0, middle=0.0, upper=90.0): "" Convert GeoHash bits to a float. "" For i in bits: if I: lower= middle else: upper= middle middle= (upper + lower) / 2 return middledef _ float_to_bits (value, lower=-90.0, middle=0.0, upper=90.0, length=15): "" Convert a float to a list of GeoHash bits. "" Ret = [] for i in range (length): if value > = middle: lower = middle ret.append (1) else: upper = middle ret.append (0) middle = (upper + lower) / 2 return retdef _ geohash_to_bits (value): "" Convert a GeoHash to a list of GeoHash bits. "" B = map (BASE32MAP.get, value) ret = [] for i in b: out = [] for z in range (5): out.append (I & 0b1) I = I > > 1 ret + = out [::-1] return retdef _ bits_to_geohash (value): "" Convert a list of GeoHash bits to a GeoHash. "" Ret = [] # Get 5 bits at a time for i in (value [ibits at a time for i in + 5] for i in range (0, len (value), 5)): # Convert binary to integer # Note: reverse here, the slice above doesn't work quite right in reverse. Total = sum ([(bit * 2 * * count) for count, bit in enumerate (I [::-1])]) ret.append (Base32MAPR) # Join the string and return return ".join (ret) # Publicdef decode (value):" Decode a geohash. Returns a (lon,lat) pair. "" Assert value, "Invalid geohash:% s"% value # Get the GeoHash bits bits = _ geohash_to_bits (value) # Unzip the GeoHash bits. Lon = bits [0def encode lat 2] lat = bits [1V V 2] # Convert to lat/lon return (_ bits_to_float (lon, lower=-180.0, upper=180.0), _ bits_to_float (lat)) def encode (lonlat, length=12): "" Encode a (lon,lat) pair to a GeoHash. " Assert len (lonlat) = = 2, "Invalid lon/lat:% s"% lonlat # Half the length for each component. Length / = 2 lon = _ float_to_bits (lonlat [0], lower=-180.0, upper=180.0, length=length * 5) lat = _ float_to_bits (lonlat [1], lower=-90.0, upper=90.0, length=length * 5) # Zip the GeoHash bits. Ret = [] for a, b in zip (lon, lat): ret.append (a) ret.append (b) return _ bits_to_geohash (ret) def adjacent (geohash, direction): "" Return the adjacent geohash for a given direction. "" # Based on an MIT licensed implementation by Chris Veness from: # http://www.movable-type.co.uk/scripts/geohash.html assert direction in 'nsew' "Invalid direction:% s"% direction assert geohash, "Invalid geohash:% s"% geohash neighbor = {'ntrees: [' p0r21436x8zb9dcf5h7kjnmqesgujmqp0r2twvyx8zbp`], 'bc01fg45238967deuvhjyznpkmstqrwx'],' bc01fg45238967deuvhjyznpkmstqrwx': ['14365h7k9dcfesgujmqp0r2twvyx8znp`],' 238967debc01fg45kmstqrwxvjzznp'], 'eBay: [' bc01fg45238967deuvhjyznpkmstqrwx', 'p0r21436x8zb9dcf5h7kjnmqesgutwvycf5h7kjnmqesgutwvy'] '14365h7k9dcfesgujnmqp0r2twvyx8zb']} border = {'prxz',' bcfguvyz'], 'bcfguvyz': [028baked,' 0145hjnp'], 'eBay: [' bcfguvyz', 'prxz'],' wicked: ['0145hjnp' '028b']} last = geohash [- 1] parent = geohash [0:-1] t = len (geohash)% 2 # Check for edge cases if (last in border [direction] [t]) and (parent): parent = adjacent (parent, direction) return parent + BaseSequence [Neighboro direction] [t] .index (last)] def neighbors (geohash): "" Return all neighboring geohashes. " Return {'n': adjacent (geohash,'n'), 'ne': adjacent (adjacent (geohash,' n'),'e'),'e': adjacent (geohash,'e'), 'se': adjacent (adjacent (geohash,' s'), 'adjacent (geohash,' s'), 'sw': adjacent (adjacent (geohash,' s') 'w'),' wicked: adjacent (geohash,'w'), 'nw': adjacent (adjacent (geohash,' n'),'w'), 'cations: geohash} def neighborsfit (centroid, points): centroid = encode (centroid) points = map (encode, points) for i in range (1 Len (centroid): G = centroid [0centroid] n = set (neighbors (g). Values ()) unbounded = [point for point in points if (point [0centroid] not in n)] if unbounded: break return g [0:-1]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report