• SpringBoot与PostGIS整合,实现智能交通GIS系统

SpringBoot与PostGIS整合,实现智能交通GIS系统

2025-05-06 09:00:07 栏目:宝塔面板 76 阅读

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]
        }
    }
]


本文地址:https://www.yitenyun.com/180.html

搜索文章

Tags

数据库 API FastAPI Calcite 电商系统 MySQL Web 应用 异步数据库 数据同步 ACK 双主架构 循环复制 TIME_WAIT 运维 负载均衡 Deepseek 宝塔面板 Linux宝塔 Docker JumpServer JumpServer安装 堡垒机安装 Linux安装JumpServer esxi esxi6 root密码不对 无法登录 web无法登录 生命周期 序列 核心机制 SSL 堡垒机 跳板机 HTTPS HexHub Windows Windows server net3.5 .NET 安装出错 HTTPS加密 宝塔面板打不开 宝塔面板无法访问 查看硬件 Linux查看硬件 Linux查看CPU Linux查看内存 InnoDB 数据库锁 Oracle 处理机制 连接控制 机制 无法访问宝塔面板 ES 协同 监控 Windows宝塔 Mysql重置密码 Serverless 无服务器 语言 开源 PostgreSQL 存储引擎 技术 group by 索引 Spring Redis 异步化 分页查询 服务器 管理口 高可用 缓存方案 缓存架构 缓存穿透 SQL 动态查询 响应模型 自定义序列化 GreatSQL 连接数 数据 主库 SVM Embedding 日志文件 MIXED 3 云原生 服务器性能 SQLark PG DBA scp Linux的scp怎么用 scp上传 scp下载 scp命令 AI 助手 ​Redis 机器学习 推荐模型 向量数据库 大模型 R edis 线程 Netstat Linux 服务器 端口 Undo Log Linux 安全 工具 OB 单机版 存储 查询 SQLite-Web SQLite 数据库管理工具 共享锁 openHalo Rsync Recursive 电商 系统 Postgres OTel Iceberg 架构 R2DBC RocketMQ 长轮询 配置 • 索引 • 数据库 聚簇 非聚簇 数据分类 加密 修改DNS Centos7如何修改DNS redo log 重做日志 磁盘架构 流量 sftp 服务器 参数 优化 万能公式 Hash 字段 同城 双活 防火墙 黑客 Ftp 场景 信息化 智能运维 MySQL 9.3 RDB AOF MVCC 人工智能 推荐系统 数据备份 高效统计 今天这篇文章就跟大家 mini-redis INCR指令 业务 缓存 窗口 函数 网络架构 网络配置 INSERT COMPACT Redisson 锁芯 向量库 Milvus Doris SeaTunnel 线上 库存 预扣 事务 Java 开发 IT运维 核心架构 订阅机制 prometheus Alert 引擎 性能 不宕机 Python Web PostGIS B+Tree ID 字段 崖山 新版本 MongoDB 数据结构 数据脱敏 加密算法 数据类型 传统数据库 向量化 分布式 集中式 虚拟服务器 虚拟机 内存 ZODB 读写 容器 容器化 filelock JOIN Canal 网络故障 DBMS 管理系统 模型 OAuth2 Token 微软 SQL Server AI功能 QPS 高并发 Redis 8.0 自动重启 Pottery 发件箱模式 聚簇索引 非聚簇索引 Testcloud 云端自动化 分库 分表 部署 锁机制 Entity 排行榜 排序 速度 服务器中毒 事务隔离 SpringAI 分页方案 排版 工具链 Caffeine CP 启动故障 数据页 悲观锁 乐观锁 StarRocks 数据仓库 SSH 池化技术 连接池 Web 接口 sqlmock 1 数据集成工具 单点故障 Go 数据库迁移 MCP 开放协议 频繁 Codis LRU 原子性 Redka 大表 业务场景 分页 AIOPS 意向锁 记录锁 网络 分布式架构 分布式锁​ 优化器 Order EasyExcel MySQL8 IT 仪表盘 dbt 数据转换工具 日志 对象 单线程 字典 InfluxDB 双引擎 RAG HelixDB 行业 趋势 事务同步 Ansible 国产数据库 LLM UUIDv7 主键 Crash 代码 List 类型 订单 线程安全 Pump UUID ID 主从复制 代理 Next-Key 编程 Valkey Valkey8.0 关系数据库 ReadView 产业链 兼容性 语句 播客 解锁 调优 恢复数据 数据字典 失效 MGR 分布式集群 算法 国产 用户 快照读 当前读 视图 RR 互联网 GitHub Git 矢量存储 数据库类型 AI代理 查询规划 千万级 慢SQL优化 Weaviate count(*) count(主键) 行数 神经系统 表空间 分布式锁 Zookeeper 拦截器 动态代理 并发控制 恢复机制 CAS 多线程 技巧 闪回