# geohash vs PostGIS

+关注继续查看

### 标签

PostgreSQL , PostGIS , GEOHASH , 经纬度 , geometry , geography

## geohash 原理

### 如何将geohash编码转换为经纬度？

value指范围的中间值，ERR指误差。

### geohash缺陷

1. 网格化算法带来的精度问题

2. 地球是不规则椭圆，geohash的偏差会随着纬度的变大骤变，越接南北极地，距离计算越不准确。

• At the Equator (0 Degrees) the length of a degree of longitude is 111.320 km, while a degree of latitude measures 110.574 km, an error of 0.67%.

• At 30 Degrees (Mid Latitudes) the error is 110.852/96.486 = 14.89%

• At 60 Degrees (High Arctic) the error is 111.412/55.800 = 99.67%, reaching infinity at the poles.

3. 经度边界(-180,180)数据的处理，边界的哈希值完全不同，但是他们却相邻，不利于相邻计算。

## PostGIS geometry, geography, raster, ...

PostgreSQL PostGIS 发展了几十年，有非常深厚的理论背景，广泛应用于军工、科研、商业场景。

PostGIS adds extra types (geometry, geography, raster and others) to the PostgreSQL database.

It also adds functions, operators, and index enhancements that apply to these spatial types.

These additonal functions, operators, index bindings and types augment the power of the core PostgreSQL DBMS, making it a fast, feature-plenty, and robust spatial database management system.

### Feature List

The PostGIS 2+ series provides:

• Processing and analytic functions for both vector and raster data for splicing, dicing, morphing, reclassifying, and collecting/unioning with the power of SQL

• raster map algebra for fine-grained raster processing

• Spatial reprojection SQL callable functions for both vector and raster data

• Support for importing / exporting ESRI shapefile vector data via both commandline and GUI packaged tools and support for more formats via other 3rd-party Open Source tools

• Packaged command-line for importing raster data from many standard formats: GeoTiff, NetCDF, PNG, JPG to name a few

• Rendering and importing vector data support functions for standard textual formats such as KML,GML, GeoJSON,GeoHash and WKT using SQL

• Rendering raster data in various standard formats GeoTIFF, PNG, JPG, NetCDF, to name a few using SQL

• Seamless raster/vector SQL callable functions for extrusion of pixel values by geometric region, running stats by region, clipping rasters by a geometry, and vectorizing rasters

• 3D object support, spatial index, and functions

• Network Topology support

• Packaged Tiger Loader / Geocoder/ Reverse Geocoder / utilizing US Census Tiger data

## geohash vs PostGIS

http://www.cnblogs.com/LBSer/p/3629149.html

GIS系统使用 使用较少 使用较多，例如openstreetmap的数据库后台就是Postgresql+Postgis

### PostGIS不仅支持geometry,geography也支持geohash

PostGIS虽然不推荐使用geohash，但是它内置了转换函数，可以将geometry转换为geohash

#### Name

ST_GeoHash — Return a GeoHash representation of the geometry.


#### Synopsis

text ST_GeoHash(geometry geom, integer maxchars=full_precision_of_point);


#### Description

Return a GeoHash representation (http://en.wikipedia.org/wiki/Geohash) of the geometry. A GeoHash encodes a point into a text form that is sortable and searchable based on prefixing. A shorter GeoHash is a less precise representation of a point. It can also be thought of as a box, that contains the actual point.

If no maxchars is specified ST_GeoHash returns a GeoHash based on full precision of the input geometry type. Points return a GeoHash with 20 characters of precision (about enough to hold the full double precision of the input). Other types return a GeoHash with a variable amount of precision, based on the size of the feature. Larger features are represented with less precision, smaller features with more precision. The idea is that the box implied by the GeoHash will always contain the input feature.

If maxchars is specified ST_GeoHash returns a GeoHash with at most that many characters so a possibly lower precision representation of the input geometry. For non-points, the starting point of the calculation is the center of the bounding box of the geometry.

Availability: 1.4.0

#### [Note]

ST_GeoHash will not work with geometries that are not in geographic (lon/lat) coordinates.

This method supports Circular Strings and Curves

#### Examples

SELECT ST_GeoHash(ST_SetSRID(ST_MakePoint(-126,48),4326));

st_geohash
----------------------
c0w3hf1s70w3hf1s70w3

SELECT ST_GeoHash(ST_SetSRID(ST_MakePoint(-126,48),4326),5);

st_geohash
------------
c0w3h


## 参考

https://en.wikipedia.org/wiki/Geohash

http://postgis.net/docs/ST_GeoHash.html

http://www.cnblogs.com/LBSer/p/3629149.html

http://planet.postgis.net/

6380 0

3970 0

7615 0

9325 0
windows server 2008阿里云ECS服务器安全设置

4994 0

2031 0

16300 0
+关注

2153

245

+ 订阅

《SaaS模式云原生数据仓库应用场景实践》

《看见新力量：二》电子书