1. Introduction
1.绪论
In this article, we’ll understand the core architecture and important elements of a geospatial application. We’ll begin by understanding what a geospatial application is and typical challenges in building one.
在这篇文章中,我们将了解地理空间应用程序的核心架构和重要元素。我们将首先了解什么是地理空间应用,以及在建立一个地理空间应用时的典型挑战。
One of the important aspects of a geospatial application is representing useful data on intuitive maps. Although, in this article, we’ll mostly focus on handling geospatial data at the backend and the options available to us in the industry today.
地理空间应用程序的一个重要方面是在直观的地图上表示有用的数据。虽然,在这篇文章中,我们将主要关注在后端处理地理空间数据,以及当今行业中可用的选项。
2. What Are Geospatial Applications?
2.什么是地理空间应用?
Let’s begin by understanding what we mean by a geospatial application. These are basically applications that make use of geospatial data to deliver their core features.
让我们首先了解一下地理空间应用程序的含义。这些基本上是利用地理空间数据来提供其核心功能的应用程序。
In simple terms, geospatial data are any data that represent places, locations, maps, navigation, and so on. Even without any fancy definition, we’re quite surrounded by these applications. For instance, our favorite ride-sharing applications, food-delivery applications, and movie-booking applications are all geospatial applications.
简单地说,地理空间数据是代表地方、地点、地图、导航等的任何数据。即使没有任何花哨的定义,我们也被这些应用所包围。例如,我们最喜欢的共享汽车应用、食品配送应用和电影预订应用都是地理空间应用。
Geospatial data is basically information that describes objects, events, or other features with a location on or near the earth’s surface. For instance, think of an application that can suggest to us the nearest theaters playing our favorite Shakespeare plays in the evening today. It can do so by combining the location information of theaters with the attribute information of the plays and the temporal information of the events.
地理空间数据基本上是描述物体、事件或其他特征的信息,其位置在地球表面或附近。例如,想一想,一个应用程序可以向我们推荐最近的剧院,在今天晚上播放我们喜欢的莎士比亚戏剧。它可以通过将剧院的位置信息与戏剧的属性信息和事件的时间信息相结合来做到这一点。
There are several other useful applications of geospatial data that deliver day-to-day value to us — for example, when we try to locate a cab nearby at any hour of the day that is willing to take us to our destination. Or when we can’t wait for that important shipment to arrive and, thankfully, can locate where exactly it is in transit.
地理空间数据还有其他一些有用的应用,为我们提供了日常的价值–例如,当我们试图在一天中的任何时候找到附近的出租车,愿意把我们带到目的地。或者,当我们等不及那批重要的货物到达时,幸好可以找到它在运输途中的确切位置。
In fact, this has become a fundamental requirement of several applications that we use quite often these days.
事实上,这已经成为我们现在经常使用的几个应用程序的基本要求。
3. Geospatial Technologies
3.地理空间技术
Before we understand the nuances of building a geospatial application, let’s go through some of the core technologies that empower such applications. These are the underlying technologies that help us generate, process, and present geospatial data in a useful form.
在我们了解构建地理空间应用程序的细微差别之前,让我们先了解一下赋予此类应用程序的一些核心技术。这些是帮助我们生成、处理并以有用的形式呈现地理空间数据的底层技术。
Remote Sensing (RS) is the process of detecting and monitoring the physical characteristics of an area by measuring its reflected and emitted radiation at a distance. Typically this is done using remote sensing satellites. It has significant usage in the fields of surveying, intelligence, and also commercial applications.
遥感(RS)是通过测量一个地区的反射和发射的辐射来探测和监测该地区的物理特征的过程。一般来说,这是用遥感卫星完成的。它在测量、情报和商业应用等领域具有重要用途。
Global Positioning Systems (GPS) refers to a satellite-based navigation system based on a network of 24 satellites flying in the Medium Earth Orbit (MEO). It can provide geolocation and time information to a suitable receiver anywhere on the Earth where it has an unobstructed line-of-sight to four or more GPS satellites.
全球定位系统(GPS)是一个基于卫星的导航系统,由24颗卫星组成的网络在中地球轨道(MEO)飞行。它可以向地球上任何地方的合适的接收器提供地理位置和时间信息,只要它与四颗或更多的GPS卫星有无障碍的视线。
Geographic Information Systems (GIS) is a system that creates, manages, analyzes, and maps all types of data. For instance, it helps us to integrate location data with more descriptive information like what is present in that location. It helps improve communication and decision-making in several industries.
地理信息系统(GIS)是一个能够创建、管理、分析和绘制所有类型数据的系统。例如,它帮助我们将位置数据与更多的描述性信息相结合,如该地点存在的东西。它有助于改善几个行业的沟通和决策。
4. Challenges in Building a Geospatial Application
4.构建地理空间应用的挑战
To understand what design choices we should make when building a geospatial application, it’s important to know the challenges involved. Typically, geospatial applications require real-time analysis of a large volume of geospatial data. For instance, finding the quickest alternate route to a recent hit place with a natural disaster is crucial for first responders.
要了解我们在构建地理空间应用程序时应该做出什么样的设计选择,就必须了解其中的挑战。通常,地理空间应用程序需要实时分析大量的地理空间数据。例如,为最近遭受自然灾害的地方寻找最快捷的备用路线,这对第一反应者来说至关重要。
So basically, one of the underpinning requirements of a geospatial application is storing tons of geospatial data and facilitating arbitrary queries with very low latency. Now, it’s also important to understand the nature of spatial data and why it requires special handling. Basically, spatial data represents objects defined in geometric space.
因此,基本上,地理空间应用程序的基础要求之一是存储大量的地理空间数据,并以极低的延迟促进任意的查询。现在,理解空间数据的性质以及为什么它需要特殊处理也很重要。基本上,空间数据表示在几何空间中定义的对象。
Let’s imagine that we have several locations of interest around a city. A location is typically described by its latitude, longitude, and (possibly) elevation:
让我们想象一下,我们在一个城市周围有几个感兴趣的地点。一个地点通常由其纬度、经度和(可能)海拔来描述。
Now, what we’re really interested in is to find the nearby locations to a given location. So, we need to compute the distance from this location to all the possible locations. Such queries are quite atypical of regular database queries we’re familiar with. These are known as spatial queries. They involve geometric data types and consider the spatial relationship between these geometries.
现在,我们真正感兴趣的是找到一个给定地点的附近位置。因此,我们需要计算从这个位置到所有可能的位置的距离。这样的查询与我们熟悉的常规数据库查询很不一样。这些是著名的空间查询。它们涉及几何数据类型,并考虑这些几何图形之间的空间关系。
We already know that no production database would probably survive without efficient indices. That’s also true for spatial data. However, due to its nature, regular indices would not be very efficient for spatial data and the types of spatial queries we want to perform. Hence, we need specialized indices known as spatial indices that can help us perform spatial operations more efficiently.
我们已经知道,如果没有高效的索引,任何生产型数据库都可能无法生存。这对空间数据来说也是如此。然而,由于其性质,常规索引对于空间数据和我们想要执行的空间查询类型来说并不是非常有效的。因此,我们需要被称为空间索引的专门索引,可以帮助我们更有效地执行空间操作。
5. Spatial Data Types and Queries
5.空间数据类型和查询
Now that we understand the challenges in dealing with spatial data, it’s important to note several types of spatial data. Moreover, we can perform several interesting queries on them to serve unique requirements. We’ll cover some of these data types and the operations we can perform on them.
现在我们了解了处理空间数据的挑战,重要的是要注意到空间数据的几种类型。此外,我们可以对它们进行一些有趣的查询,以满足独特的要求。我们将介绍这些数据类型中的一些,以及我们可以对它们进行的操作。
We normally talk about spatial data with respect to a spatial reference system. This is composed of a coordinate system and a datum. There are several coordinate systems like affine, cylindrical, cartesian, ellipsoidal, linear, polar, spherical, and vertical. A datum is a set of parameters that define the position of the origin, the scale, and the orientation of a coordinate system.
我们通常在谈论空间数据时,会涉及到一个空间参考系统。这是由一个坐标系和一个基准点组成的。有几种坐标系统,如仿生、圆柱、卡特尔、椭圆、线性、极地、球面和垂直。基准是一组参数,定义了坐标系的原点、比例和方向的位置。
Broadly speaking, many databases supporting spatial data divide them into two categories, geometry, and geography:
广义上讲,许多支持空间数据的数据库将它们分为两类,即几何学和地理学。
Geometry stores spatial data on a flat coordinate system. This helps us represent shapes like points, lines, and regions with their coordinates in cartesian space. Geography stores spatial data based on a round-earth coordinate system. This is useful in representing the same shapes on the surface of the Earth with latitude and longitude coordinates.
几何学将空间数据存储在一个平面坐标系上。这有助于我们在笛卡尔空间中用坐标表示点、线和区域等形状。地理学在圆地球坐标系的基础上存储空间数据。这对于用经纬度坐标表示地球表面的相同形状很有用。
There are two fundamental types of inquiry we can make with spatial data. These are basically to find nearest neighbors or to send different types of range queries. We’ve already seen examples of queries to find the nearest neighbors earlier. The general idea is to identify a certain number of items nearest to a query point.
我们可以对空间数据进行两种基本类型的查询。这些基本上是寻找最近的邻居或发送不同类型的范围查询。我们在前面已经看到了寻找最近的邻居的查询的例子。一般的想法是确定一定数量的离查询点最近的项目。
The other important type of query is the range query. Here, we’re interested to know all the items that fall within a query range. The query range can be a rectangle or a circle with a certain radius from a query point. For instance, we can use this kind of query to identify all Italian restaurants within a two-mile radius from where we’re standing.
另一种重要的查询类型是范围查询。在这里,我们有兴趣知道所有属于查询范围的项目。查询范围可以是一个矩形或一个以查询点为半径的圆。例如,我们可以用这种查询来确定从我们所站的地方到两英里范围内的所有意大利餐馆。
6. Data Structures for Spatial Data
6.空间数据的数据结构
Now, we’ll understand some of the data structures that are more appropriate for building spatial indices. This will help us understand how they are different from regular indices and why they’re more efficient in handling spatial operations. Invariably, almost all these data structures are variations of the tree data structure.
现在,我们将了解一些更适合建立空间索引的数据结构。这将有助于我们理解它们与普通索引有什么不同,以及为什么它们在处理空间操作时更有效率。无独有偶,几乎所有这些数据结构都是树形数据结构的变种。
6.1. Regular Database Index
6.1.常规数据库索引
A database index is basically a data structure that improves the speed of data retrieval operations. Without an index, we would have to go through all the rows to search for the row we’re interested in. But, for a table of significant size, even going through the index can take a significant amount of time.
数据库索引基本上是一种数据结构,可以提高数据检索操作的速度。如果没有索引,我们将不得不通过所有的行来搜索我们感兴趣的行。但是,对于一个相当大的表来说,即使翻阅索引也会花费大量的时间。
However, it’s important to reduce the number of steps to fetch a key and reduce the number of disk operations to do so. A B-tree or a balanced tree is a self-balancing tree data structure that stores several sorted key-value pairs in every node. This helps pull a larger set of keys in the processor cache in a single read operation from the disk.
然而,重要的是要减少获取一个键的步骤,并减少为此进行的磁盘操作的数量。B树或平衡树是一种自我平衡的树形数据结构,它在每个节点中都存储了几个排序的键值对。这有助于在一次从磁盘读取操作中,在处理器缓存中拉出更大的键集。
While a B-tree works pretty well, generally, we use a B+tree for building a database index. A B+tree is very similar to a B-tree except for the fact that it stores values or data only at the leaf nodes:
虽然B树的效果相当好,但一般来说,我们使用B+树来建立数据库索引。B+树与B树非常相似,只是它只在叶子节点上存储数值或数据。
Here, all the leaf nodes are also linked and, hence, provide ordered access to the key-value pairs. The benefit here is that the leaf nodes provide the first level of the index, while the internal nodes provide a multilevel index.
在这里,所有的叶子节点也被链接,因此,提供了对键值对的有序访问。这里的好处是,叶子节点提供了第一层的索引,而内部节点则提供了多级索引。
A regular database index focuses on ordering its keys on a single dimension. For instance, we can create an index on one of the attributes like zip code in our database table. This will help us to query all locations with a certain zip code or within a range of zip codes.
一个普通的数据库索引侧重于在一个单一维度上对其键进行排序。例如,我们可以在我们的数据库表中的一个属性上创建一个索引,如邮政编码。这将帮助我们查询所有具有某个邮编或在某个邮编范围内的地点。
6.2. Spatial Database Index
6.2.空间数据库索引
In geospatial applications, we’re often interested in nearest neighbor or range queries. For instance, we may want to find all locations within 10 miles of a particular point. A regular database index does not prove to be very useful here. In fact, there are other more suitable data structures to build spatial indices.
在地理空间应用中,我们经常对最近的邻居或范围查询感兴趣。例如,我们可能想找到一个特定点10英里内的所有地点。事实证明,一个普通的数据库索引在这里并不十分有用。事实上,还有其他更合适的数据结构来建立空间索引。
One of the most commonly used data structures is the R-tree. The R-tree was first proposed by Antonin Guttman in 1984 and is suitable for storing spatial objects like locations. The fundamental idea behind R-tree is to group nearby objects and represent them with their minimum bounding rectangle in the next higher level of the tree:
最常用的数据结构之一是R-树。R树是由Antonin Guttman在1984年首次提出的,适用于存储空间对象,如位置。R-tree背后的基本理念是将附近的对象分组,并在树的下一个更高层次中用它们的最小边界矩形表示它们。
For most operations, an R-tree is not very different from a B-tree. The key difference is in using the bounding rectangles to decide whether to search inside a subtree or not. For better performance, we should ensure that rectangles don’t cover too much empty space and that they don’t overlap too much. Most interestingly, an R-tree can extend to cover three or even more dimensions!
对于大多数操作来说,R树和B树没有什么不同。关键的区别在于使用边界矩形来决定是否在子树内部搜索。为了获得更好的性能,我们应该确保矩形不覆盖太多空隙,并且不发生太多重叠。最有趣的是,R树可以扩展到覆盖三个甚至更多的维度!
Another data structure for building a spatial index is the Kd-tree, which is a slight variation of the R-tree. The Kd-tree splits the data space into two instead of partitioning it into multiple rectangles. Hence, the tree nodes in a Kd-tree represent separating planes and not bounding boxes. While Kd-tree proves to be easier to implement and is faster, it’s not suitable for data that is always changing.
另一种用于建立空间索引的数据结构是Kd树,它是R树的一个轻微变化。Kd-tree将数据空间一分为二,而不是将其划分为多个矩形。因此,Kd-tree中的树节点代表分离平面,而不是边界框。虽然事实证明Kd-tree更容易实现,而且速度更快,但它不适合于总是在变化的数据。
The key idea behind these data structures is basically partitioning data into axis-aligned regions and storing them in tree nodes. In fact, there are quite a few other such data structures that we can use, like BSP-tree and R*-tree.
这些数据结构的关键思想基本上是将数据划分为轴对齐的区域,并将其存储在树形节点中。事实上,我们还可以使用其他一些这样的数据结构,比如BSP-树和R*-树。
7. Databases with Native Support
7.具有本地支持的数据库
We’ve already seen how spatial data differ from regular data and why they need special treatment. Hence, what we require to build a geospatial application is a database that can natively support storing spatial data types and that can perform spatial queries efficiently. We refer to such a database management system as a spatial database management system.
我们已经看到了空间数据与普通数据的区别,以及为什么它们需要特殊处理。因此,我们建立一个地理空间应用程序所需要的是一个能够原生支持存储空间数据类型的数据库,并且能够有效地执行空间查询。我们把这样的数据库管理系统称为空间数据库管理系统。
Almost all mainstream databases have started to provide some level of support for spatial data. This includes some popular database management systems like MySQL, Microsoft SQL Server, PostgreSQL, Redis, MongoDB, Elasticsearch, and Neo4J. However, there are some purpose-built spatial databases available as well, such as GeoMesa, PostGIS, and Oracle Spatial.
几乎所有的主流数据库都已经开始为空间数据提供某种程度的支持。这包括一些流行的数据库管理系统,如MySQL、Microsoft SQL Server、PostgreSQL、Redis、MongoDB、Elasticsearch,以及Neo4J。但是,也有一些专门建立的空间数据库,如GeoMesa、PostGIS和Oracle Spatial。
7.1. Redis
7.1 Redis
Redis is an in-memory data structure store that we can use as a database, a cache, or a message broker. It can minimize the network overhead and latency as it performs operations efficiently in memory. Redis supports various data structures like Hash, Set, Sorted Set, List, and String. Of particular interest for us are Sorted Sets that add an ordered view to members, sorted by scores.
Redis是一种内存数据结构存储,我们可以将其用作数据库、缓存或消息代理。它可以最大限度地减少网络开销和延迟,因为它在内存中有效地执行操作。Redis支持各种数据结构,如哈希、集合、排序集合、列表和字符串。我们特别感兴趣的是排序集,它为成员添加了一个有序的视图,按分数排序。
Geospatial indexing is implemented in Redis using Sorted Sets as the underlying data structure. Redis actually encodes the latitude and longitude into the score of the Sorted Set using the geohash algorithm. Geo Set is the key data structure implemented with a Sorted Set and supports geospatial data in Redis at a more abstract level.
地理空间索引是在Redis中使用排序集作为底层数据结构实现的。Redis实际上使用地理哈希算法将经纬度编码到排序集的分值中。Geo Set是用Sorted Set实现的关键数据结构,在更抽象的层面上支持Redis中的地理空间数据。
Redis provides simple commands to work with the geospatial index and perform common operations like creating new sets and adding or updating members in the set. For instance, to create a new set and add members to it from the command line, we can use the GEOADD command:
Redis提供了简单的命令来处理地理空间索引,并执行常见的操作,如创建新的集合和添加或更新集合中的成员。例如,要从命令行中创建一个新集并向其中添加成员,我们可以使用GEOADD命令。
GEOADD locations 20.99 65.44 Vehicle-1 23.99 55.45 Vehicle-2
Here, we’re adding the location of a few vehicles to a Geo Set called “locations”.
在这里,我们要把一些车辆的位置添加到一个名为 “地点 “的地理集里。
Redis also provides several ways to read the index, like ZRANGE, ZSCAN, and GEOPOS. Further, we can use the command GEODIST to compute the distance between the members in a set. But the most interesting commands are those that allow us to search the index by location. For instance, we can use the command GEORADIUSYMEMBER to search members that are within a radius range of a particular member:
Redis还提供了几种读取索引的方法,如ZRANGE、ZSCAN和GEOPOS。此外,我们可以使用GEODIST命令来计算一个集合中成员之间的距离。但是最有趣的命令是那些允许我们通过位置来搜索索引的命令。例如,我们可以使用GEORADIUSYMEMBER命令来搜索在某个特定成员的半径范围内的成员。
GEORADIUSBYMEMBER locations Vehicle-3 1000 m
Here, we’re interested in finding all other vehicles within a one-kilometer radius of the third vehicle.
这里,我们感兴趣的是找到第三辆车一公里半径内的所有其他车辆。
Redis is quite powerful and simple in providing support for storing a large volume of geospatial data and performing low latency geospatial queries.
Redis在为存储大量的地理空间数据和执行低延迟的地理空间查询提供支持方面相当强大和简单。
7.2. MongoDB
7.2 MongoDB
MongoDB is a document-oriented database management system that uses JSON-like documents with an optional schema to store data. It provides several ways to search documents, like field queries, range queries, and regular expressions. We can even index the documents to primary and secondary indices. Moreover, MongoDB with sharding and replication provides high availability and horizontal scalability.
MongoDB是面向文档的数据库管理系统,它使用类似JSON的文档和可选模式来存储数据。它提供了几种搜索文档的方法,如字段查询、范围查询和正则表达式。我们甚至可以对文档进行主索引和次索引。此外,具有分片和复制功能的MongoDB提供了高可用性和水平扩展性。
We can store spatial data in MongoDB either as GeoJSON objects or as legacy coordinate pairs. GeoJSON objects are useful for storing location data over an Earth-like surface, whereas legacy coordinate pairs are useful for storing data that we can represent in a Euclidean plane.
我们可以在MongoDB中以GeoJSON对象或传统坐标对的形式存储空间数据。GeoJSON对象对于存储类似地球表面的位置数据很有用,而传统坐标对对于存储我们可以在欧几里得平面上表示的数据很有用。
To specify the GeoJSON data, we can use an embedded document with a field named type to indicate GeoJSON object type and another field named coordinates to indicate the object’s coordinates:
为了指定GeoJSON数据,我们可以使用一个嵌入式文档,其中有一个名为type的字段来表示GeoJSON对象的类型,还有一个名为coordinates的字段来表示该对象的坐标。
db.vehicles.insert( {
name: "Vehicle-1",
location: { type: "Point", coordinates: [ 83.97, 70.77 ] }
} )
Here, we’re adding a document in the collection named vehicles. The embedded document is a GeoJSON object of the type Point with its longitude and latitude coordinates.
在这里,我们要在集合中添加一个名为vehicles的文档。嵌入的文件是一个GeoJSON对象,其类型为Point,有其经度和纬度坐标。
Further, MongoDB provides multiple geospatial index types like 2dsphere and 2d to support geospatial queries. A 2dsphere supports queries that calculate geometries on an Earth-like sphere:
此外,MongoDB 提供了多种地理空间索引类型,如2dsphere和2d以支持地理空间查询。2dsphere支持在一个类似地球的球体上计算几何图形的查询。
db.vehicles.createIndex( { location : "2dsphere" } )
Here, we’re creating a 2dsphere index on the location field of our collection.
在这里,我们在集合的location字段上创建一个2dsphere索引。
Lastly, MongoDB offers several geospatial query operators to facilitate searching through the geospatial data. Some of the operators are geoIntersects, geoWithin, near, and nearSphere. These operators can interpret geometry on a flat surface or a sphere.
最后,MongoDB 提供了几个地理空间查询运算符,以方便通过地理空间数据进行搜索。其中一些运算符是geoIntersects、geoWithin、near和nearSphere。这些运算符可以解释平坦的表面或球体上的几何图形。
For instance, let’s see how we can use the near operator:
例如,让我们看看我们如何使用near操作符。
db.places.find(
{
location:
{ $near:
{
$geometry: { type: "Point", coordinates: [ 93.96, 30.78 ] },
$minDistance: 500,
$maxDistance: 1000
}
}
}
)
Here, we’re searching for documents that are at least 500 meters and at most 1,000 meters from the mentioned GeoJSON Point.
在这里,我们要搜索的是距离所述GeoJSON Point至少500米、最多1000米的文件。
The power of representing JSON-like data with flexible schema, scale efficiency, and inherent support for geospatial data makes MongoDB quite suitable for geospatial applications.
表达类似JSON的数据的能力与灵活的模式、规模效率和对地理空间数据的内在支持,使得MongoDB相当适合地理空间应用。
7.3. PostGIS
7.3. PostGIS
PostgreSQL is a relational database management system that provides SQL compliance and features ACID transactions. It’s quite versatile in supporting a wide variety of workloads. PostgreSQL includes built-in support for synchronous replication and built-in support for regular B-tree and hash table indices. PostGIS is a spatial database extender for PostgreSQL.
PostgreSQL是一种关系型数据库管理系统,它提供SQL兼容性并具有ACID事务。它在支持各种工作负载方面的功能相当全面。PostgreSQL包括对同步复制的内置支持以及对常规B树和哈希表索引的内置支持。PostGIS是PostgreSQL的一个空间数据库扩展器。
Basically, PostGIS adds support for storing geospatial data in PostgreSQL and executing location queries in SQL. It adds geometry types for Points, LineStrings, Polygons, and more. Further, it provides spatial indices using R-tree-over-GiST (Generalized Search Tree). Lastly, it also adds spatial operators for geospatial measurements and set operations.
基本上,PostGIS增加了对在PostgreSQL中存储地理空间数据的支持以及在SQL中执行位置查询。它增加了Points、LineStrings、Polygons等的几何类型。此外,它使用R-tree-over-GiST(通用搜索树)提供空间索引。最后,它还为地理空间测量和集合操作增加了空间运算符。
We can create a database in PostgreSQL as always and enable the PostGIS extension to start using it. Fundamentally, data is stored in rows and columns, but PostGIS introduces a geometry column with data in a specific coordinate system defined by a Spatial Reference Identifier (SRID). PostGIS also adds many options for loading different GIS data formats.
我们可以像往常一样在PostgreSQL中创建一个数据库并启用PostGIS扩展来开始使用它。从根本上说,数据是以行和列的形式存储的,但PostGIS引入了一个几何列,其中的数据是由空间参考标识符(SRID)定义的特定坐标系。PostGIS还增加了许多选项来加载不同的GIS数据格式。
PostGIS supports both geometry and geography data types. We can use regular SQL queries to create a table and insert a geography data type:
PostGIS同时支持几何和地理数据类型。我们可以使用常规的SQL查询来创建一个表并插入一个地理数据类型。
CREATE TABLE vehicles (name VARCHAR, geom GEOGRAPHY(Point));
INSERT INTO vehicles VALUES ('Vehicle-1', 'POINT(44.34 82.96)');
Here, we’ve created a new table “vehicles” and have added the location of a particular vehicle using the Point geometry.
在这里,我们已经创建了一个新的表 “车辆”,并使用Point几何图形添加了一个特定车辆的位置。
PostGIS adds quite a few spatial functions to perform spatial operations on the data. For instance, we can use spatial function ST_AsText to read geometry data as text:
PostGIS增加了相当多的空间函数来对数据进行空间操作。例如,我们可以使用空间函数ST_AsText来读取几何数据的文本。
SELECT name, ST_AsText(geom) FROM vehicles;
Of course, for us, a more useful query is to look for all vehicles in the near vicinity of a given point:
当然,对我们来说,一个更有用的查询是寻找某个点附近的所有车辆。
SELECT geom FROM vehicles
WHERE ST_Distance( geom, 'SRID=4326;POINT(43.32 68.35)' ) < 1000
Here, we’re searching for all vehicles within a one-kilometer radius of the provided point.
在这里,我们要搜索所提供的点的一公里半径内的所有车辆。
PostGIS adds the spatial capabilities to PostgreSQL, allowing us to leverage well-known SQL semantics for spatial data. Moreover, we can benefit from all the advantages of using PostgreSQL.
PostGIS为PostgreSQL增加了空间功能,使我们能够利用众所周知的SQL语义来处理空间数据。此外,我们可以从使用PostgreSQL的所有优势中获益。
8. Industry Standards and Specifications
8.行业标准和规范
While we’ve seen that the support for spatial data is growing in the database layer, what about the application layer? For building geospatial applications, we need to write code that is capable of handling spatial data efficiently.
虽然我们看到数据库层对空间数据的支持在不断增加,但应用层呢?对于构建地理空间应用程序,我们需要编写能够有效处理空间数据的代码。
Moreover, we need standards and specifications to represent and transfer spatial data between different components. Further, language bindings can support us in building a geospatial application in a language like Java.
此外,我们需要标准和规范来表示和传输不同组件之间的空间数据。此外,语言绑定可以支持我们在像Java这样的语言中建立地理空间应用。
In this section, we’ll cover some of the standardization that has taken place in the field of geospatial applications, the standards they have produced, and the libraries that are available for us to use.
在本节中,我们将介绍一些在地理空间应用领域发生的标准化,他们所产生的标准,以及可供我们使用的库。
8.1. Standardization Efforts
8.1.标准化的努力
There has been a lot of development in this area, and through the collaborative efforts of multiple organizations, several standards, and best practices have been established. Let’s first go through some of the organizations contributing to the advancement and standardization of geospatial applications across different industries.
在这个领域已经有很多发展,通过多个组织的合作努力,已经建立了一些标准和最佳实践。让我们先来看看一些为不同行业的地理空间应用的进步和标准化作出贡献的组织。
Environmental Systems Research Institute (ESRI) is perhaps one of the oldest and largest international suppliers of Geographic Information System (GIS) software and geodatabase management applications. They develop a suite of GIS software under the name ArcGIS targeted for multiple platforms like desktop, server, and mobile. It has also established and promotes data formats for both vector and raster data types — for instance, Shapefile, File Geodatabase, Esri Grid, and Mosaic.
环境系统研究所(ESRI)可能是地理信息系统(GIS)软件和地理数据库管理应用程序的最古老和最大的国际供应商之一。他们开发了一套以ArcGIS为名的GIS软件,针对桌面、服务器和移动等多个平台。它还建立并推广了矢量和光栅数据类型的数据格式–例如,Shapefile、File Geodatabase、Esri Grid和Mosaic。
Open Geospatial Consortium (OGC) is an international industry consortium of more than 300 companies, government agencies, and universities participating in a consensus process to develop publicly available interface specifications. These specifications enable complex spatial information and services accessible and useful to all kinds of applications. Currently, the OGC standard comprises more than 30 standards, including Spatial Reference System Identifier (SRID), Geography Markup Language (GML), and Simple Features – SQL (SFS).
Open Geospatial Consortium (OGC)是一个由300多家公司、政府机构和大学组成的国际产业联盟,参与制定公开的接口规范的共识过程。这些规范使复杂的空间信息和服务能够被所有类型的应用所访问和使用。目前,OGC标准包括30多个标准,包括空间参考系统标识符(SRID)、地理标记语言(GML)和简单特征-SQL(SFS)。
Open Source Geospatial Foundation (OSGeo) is a non-profit, non-government organization that supports and promotes the collaborative development of open geospatial technologies and data. It promotes geospatial specifications like Tile Map Service (TMS). Moreover, it also helps in the development of several geospatial libraries like GeoTools and PostGIS. It also works on applications like QGIS, a desktop GIS for data viewing, editing, and analysis. These are just a few of the projects that OSGeo promotes under its umbrella.
开源地理空间基金会(OSGeo)是一个非营利、非政府组织,支持和促进开放地理空间技术和数据的合作开发。它提倡像瓦片地图服务(TMS)这样的地理空间规范。此外,它还帮助开发一些地理空间库,如GeoTools和PostGIS。它还致力于开发QGIS等应用程序,这是一个用于数据查看、编辑和分析的桌面GIS。这些只是OSGeo在其保护伞下推动的几个项目。
8.2. Geospatial Standards: OGC GeoAPI
8.2.地理空间标准 OGC GeoAPI
The GeoAPI Implementation Standard defines, through the GeoAPI library, a Java language API including a set of types and methods that we can use to manipulate geographic information. The underlying structure of the geographic information should follow the specification adopted by the Technical Committee 211 of the International Organization for Standardization (ISO) and by the OGC.
GeoAPI实施标准通过GeoAPI库定义了一个Java语言API,包括一组我们可以用来操作地理信息的类型和方法。地理信息的基础结构应遵循国际标准化组织(ISO)211技术委员会和OGC所采用的规范。
GeoAPI provides Java interfaces that are implementation neutral. Before we can actually use GeoAPI, we have to pick from a list of third-party implementations. We can perform several geospatial operations using the GeoAPI. For instance, we can get a Coordinate Reference System (CRS) from an EPSG code. Then, we can perform a coordinate operation like map projection between a pair of CRSs:
GeoAPI提供的Java接口是实现中立的。在我们真正使用GeoAPI之前,我们必须从第三方的实现列表中挑选。我们可以使用GeoAPI进行若干地理空间操作。例如,我们可以从EPSG代码中获得一个坐标参考系统(CRS)。然后,我们可以执行一个坐标操作,如一对CRS之间的地图投影。
CoordinateReferenceSystem sourceCRS =
crsFactory.createCoordinateReferenceSystem("EPSG:4326"); // WGS 84
CoordinateReferenceSystem targetCRS =
crsFactory.createCoordinateReferenceSystem("EPSG:3395"); // WGS 84 / World Mercator
CoordinateOperation operation = opFactory.createOperation(sourceCRS, targetCRS);
double[] sourcePt = new double[] {
27 + (59 + 17.0 / 60) / 60, // 27°59'17"N
86 + (55 + 31.0 / 60) / 60 // 86°55'31"E
};
double[] targetPt = new double[2];
operation.getMathTransform().transform(sourcePt, 0, targetPt, 0, 1);
Here, we’re using the GeoAPI to perform a map projection to transform a single CRS point.
在这里,我们使用GeoAPI来执行地图投影来转换一个CRS点。
There are several third-party implementations of GeoAPI available as wrappers around existing libraries — for instance, NetCDF Wrapper, Proj.6 Wrapper, and GDAL Wrapper.
有几个第三方实现的GeoAPI可作为现有库的包装器,例如NetCDF包装器、Proj.6包装器和GDAL包装器。
8.3. Geospatial Libraries: OSGeo GeoTools
8.3.地理空间库 OSGeo GeoTools
GeoTools is an OSGeo project that provides an open-source Java library for working with geospatial data. The GeoTools data structure is basically based on the OGC specifications. It defines interfaces for key spatial concepts and data structures. It also comes with a data access API supporting feature access, a stateless low memory rendered, and a parsing technology using XML schema to bind to GML content.
GeoTools是一个OSGeo项目,提供了一个用于处理地理空间数据的开源Java库。GeoTools的数据结构基本上是基于OGC的规范。它定义了关键空间概念和数据结构的接口。它还配备了一个支持特征访问的数据访问API,一个无状态的低内存渲染,以及一个使用XML模式与GML内容绑定的解析技术。
To create geospatial data in GeoTools, we need to define a feature type. The simplest way is to use the class SimpleFeatureType:
为了在GeoTools中创建地理空间数据,我们需要定义一个特征类型。最简单的方法是使用SimpleFeatureType类。
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("Location");
builder.setCRS(DefaultGeographicCRS.WGS84);
builder
.add("Location", Point.class);
.length(15)
.add("Name", String.class);
SimpleFeatureType VEHICLE = builder.buildFeatureType();
Once we have our feature type ready, we can use this to create the feature with SimpleFeatureBuilder:
一旦我们准备好了我们的特征类型,我们就可以用这个来用SimpleFeatureBuilder创建这个特征。
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(VEHICLE);
DefaultFeatureCollection collection = new DefaultFeatureCollection();
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
We’re also instantiating the collection to store our features and the GeoTools factory class to create a Point for the location. Now, we can add specific locations as features to our collection:
我们也在实例化集合来存储我们的特征,并使用GeoTools工厂类来为位置创建一个Point。现在,我们可以将特定的地点作为特征添加到我们的集合中。
Point point = geometryFactory.createPoint(new Coordinate(13.46, 42.97));
featureBuilder.add(point);
featureBuilder.add("Vehicle-1");
collection.add(featureBuilder.buildFeature(null))
This is just scratching the surface of what we can do with the GeoTools library. GeoTools provides support for working with both vector and raster data types. It also allows us to work with data in a standard format like shapefile.
这只是我们利用GeoTools库所能做到的表面现象。GeoTools提供了对矢量和栅格数据类型的支持。它还允许我们处理标准格式的数据,如shapefile。
9. Conclusion
9.结语
In this tutorial, we went through the basics of building a geospatial application. We discussed the nature of and challenges in building such an application. This helped us understand the different types of spatial data and data structures we can make use of.
在本教程中,我们经历了构建地理空间应用程序的基础知识。我们讨论了建立这样一个应用程序的性质和挑战。这有助于我们理解不同类型的空间数据和数据结构,我们可以利用它们。
Further, we went through some of the open-source databases with native support for storing spatial data, building spatial indices, and performing spatial operations. Lastly, we also covered some of the industry collaborations driving the standardization efforts in geospatial applications.
此外,我们还介绍了一些开源数据库,这些数据库对存储空间数据、建立空间索引和执行空间操作有本机支持。最后,我们还介绍了一些推动地理空间应用标准化工作的行业合作。