SpringBoot与PostGIS整合,实现智能交通GIS系统
PostGIS是一个开源的空间数据库扩展,专门用于增强PostgreSQL数据库以支持地理空间对象。智能交通GIS系统就是利用地理信息系统(GIS)技术来管理和分析交通数据,帮助城市管理者优化交通规划、提高交通效率并减少拥堵。
我们为什么选择PostGIS?
- 存储空间数据:PostGIS 允许你直接在关系型数据库中存储和管理空间数据,而不需要额外的文件或外部系统。
- 空间索引:通过 GIST(Generalized Search Tree)索引加速空间查询,提高性能。
- PostGIS 提供了一系列内置函数和操作符,可以高效地处理空间数据,包括距离计算、面积测量、交集检测等。
- PostGIS 支持多种行业标准和开放协议,确保数据的互操作性和可移植性。
- 免费使用:完全开源,遵循 GNU General Public License (GPL)。
- 节省预算:减少对专有 GIS 软件的依赖,降低运营成本。
哪些机构使用了PostGIS?
- 美国环境保护局:使用 PostGIS 来存储和分析环境数据,包括空气质量、水质等,以支持政策制定和公众健康保护。
- 加拿大自然资源部: 利用 PostGIS 进行土地覆盖分类、地形分析等,支持国家的自然资源管理和环境保护工作。
- 英国政府:多个部门使用 PostGIS 来管理公共服务设施的位置数据,如医院、学校、公共交通站点等。
- Uber: 使用 PostGIS 来优化司机的路线规划,提高出行效率,并进行实时交通数据分析。
- Airbnb: 使用 PostGIS 存储和查询房源的位置数据,帮助用户找到附近的住宿,并提供个性化的推荐服务。
- Mapbox : 提供的地图服务和 SDKs 基于 PostGIS,用于存储和处理大规模的地理空间数据,支持全球范围内的地图渲染和分析。
- TomTom: 使用 PostGIS 进行交通数据的存储和分析,提供实时交通更新和导航建议。
- QGIS : 一个流行的开源 GIS 软件,支持与 PostGIS 数据库的集成,允许用户在桌面环境中进行空间数据编辑和分析。
- 悉尼大学: 研究团队使用 PostGIS 分析城市扩张、土地使用变化等环境科学问题。
代码实操
4.0.0
com.example
smart-traffic-gis-system
0.0.1-SNAPSHOT
jar
SmartTrafficGisSystem
Demo project for Spring Boot with PostgreSQL and PostGIS
org.springframework.boot
spring-boot-starter-parent
2.7.5
11
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
org.postgresql
postgresql
runtime
net.postgis
postgis-jdbc
2.5.0
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/trafficdb?currentSchema=public&createDatabaseIfNotExist=true
spring.datasource.username=postgres
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect
初始化脚本
-- 启用PostGIS扩展
CREATE EXTENSION IF NOT EXISTS postgis;
-- 创建traffic_data表,包含id、name和location字段
CREATE TABLE traffic_data (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
location GEOMETRY(Point, 4326)
);
交通数据实体类
package com.example.smarttrafficsystem.model;
import org.locationtech.jts.geom.Point;
import javax.persistence.*;
/**
* 交通数据实体类,对应数据库中的traffic_data表
*/
@Entity
@Table(name = "traffic_data")
publicclass TrafficData {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; // 主键ID,自增
private String name; // 交通点名称
/**
* 地理位置字段,类型为Point,坐标系为WGS84 (EPSG:4326)
*/
@Column(columnDefinition = "geometry(Point,4326)")
private Point location;
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Point getLocation() {
return location;
}
public void setLocation(Point location) {
this.location = location;
}
}
Repository接口
package com.example.smarttrafficsystem.repository;
import com.example.smarttrafficsystem.model.TrafficData;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface TrafficDataRepository extends JpaRepository {
}
Service层
package com.example.smarttrafficsystem.service;
import com.example.smarttrafficsystem.model.TrafficData;
import com.example.smarttrafficsystem.repository.TrafficDataRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 业务逻辑层服务类,处理与交通数据相关的业务逻辑
*/
@Service
publicclass TrafficDataService {
@Autowired
private TrafficDataRepository trafficDataRepository;
/**
* 获取所有交通数据
* @return 所有交通数据列表
*/
public List getAllTrafficData() {
return trafficDataRepository.findAll();
}
/**
* 保存新的交通数据
* @param trafficData 要保存的交通数据对象
* @return 保存后的交通数据对象
*/
public TrafficData saveTrafficData(TrafficData trafficData) {
return trafficDataRepository.save(trafficData);
}
}
Controller层
package com.example.smarttrafficsystem.controller;
import com.example.smarttrafficsystem.model.TrafficData;
import com.example.smarttrafficsystem.service.TrafficDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/v1/traffic")
publicclass TrafficController {
@Autowired
private TrafficDataService trafficDataService;
/**
* 获取所有交通数据
* @return 所有交通数据列表
*/
@GetMapping("/")
public List getAllTrafficData() {
return trafficDataService.getAllTrafficData();
}
/**
* 创建新的交通数据
* @param trafficData 要创建的交通数据对象
* @return 创建后的交通数据对象
*/
@PostMapping("/")
public TrafficData createTrafficData(@RequestBody TrafficData trafficData) {
return trafficDataService.saveTrafficData(trafficData);
}
}
Application
package com.example.smarttrafficsystem;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SmartTrafficSystemApplication {
public static void main(String[] args) {
SpringApplication.run(SmartTrafficSystemApplication.class, args);
}
}
测试
$ curl -X POST http://localhost:8080/api/v1/traffic/
-H "Content-Type: application/json"
-d '{"name": "New Traffic Point", "location": {"type": "Point", "coordinates": [125.678, 80.123]}}'
Respons:
{
"id": 1,
"name": "New Traffic Point",
"location": {
"type": "Point",
"coordinates": [125.678, 80.123]
}
}
$ curl -X GET http://localhost:8080/api/v1/traffic/
Respons:
[
{
"id": 1,
"name": "New Traffic Point",
"location": {
"type": "Point",
"coordinates": [125.678, 80.123]
}
}
]