postgis

【pgAdmin4】创建/删除:数据库Database和数据库表Table_pgadmin创建本地数据库-CSDN博客

1、增加POSTGIS扩展插件
CREATE EXTENSION postgis;

2、增加一个三维数据表

CREATE TABLE threed_points (
    id SERIAL PRIMARY KEY,
    name TEXT,
    location GEOMETRY(PointZ, 4326)  -- 使用PointZ类型表示三维点,并指定EPSG:4326坐标系
);

3、插入数据 
-- 插入一些三维点数据
INSERT INTO threed_points (name, location) VALUES 
('point1', ST_GeomFromText('POINTZ(0 0 1)', 4326)),  -- 插入一个三维点,坐标为(0, 0, 1)
('point2', ST_GeomFromText('POINTZ(1 1 2)', 4326));   -- 插入另一个三维点,坐标为(1, 1, 2)

 
-- 插入一些三维点数据
INSERT INTO threed_points (name, location) VALUES 
('point10', ST_GeomFromText('POINTZ(-10 10 0)', 4326)),  -- 插入一个三维点,坐标为(0, 0, 1)
('point100', ST_GeomFromText('POINTZ(-100 100 3)', 4326)),

('point-2', ST_GeomFromText('POINTZ(-2 -2 0)', 4326)),  -- 插入一个三维点,坐标为(0, 0, 1)
('point-8', ST_GeomFromText('POINTZ(-8 -6 3)', 4326));   -- 插入另一个三维点,坐标为(1, 1, 2)

4、sql查询

PostgresSQL中的常用空间函数_pgsql st函数-CSDN博客

SELECT id, name, location
FROM threed_points
WHERE ST_3DDistance(location, ST_GeomFromText('POINTZ(5 5 5)', 4326)) <= 5;

1.ST_GeomFromText
ST_GeomFromText函数根据字符串表示构造几何
示例如下:

ST_Geometry: ST_GeomFromText(WKT, WKID)
# ST_Geometry :返回值类型
# WKT(well-known-text):几何文本的字符串
# WKID 投影坐标对应的编号

2.ST_CoveredBy
ST_CoveredBy:测试A几何是否在空间上被B几何压覆
示例如下:

boolean: ST_CoveredBy(geomA, geomB)

3.ST_Force2D
ST_Force2D:强制几何图形进入“二维模式”,以便所有输出表示将只有X和Y坐标
示例如下:

ST_Force2D(geometry)

4.ST_Intersection
ST_Intersection:返回两个几何点集交集的几何
示例如下:

ST_Geometry : ST_Intersection(geom1, geom2)

5.st_transform
st_transform:坐标系统之间的几何转换
示例如下

st_transform(geom, WKID)

6.st_area6.st_area
计算几何面积
示例如下:

st_area6(geometry)

2.OGC标准函数
1.管理函数:
添加几何字段 AddGeometryColumn(, , , , , )
删除几何字段 DropGeometryColumn(, , )
检查数据库几何字段并在geometry_columns中归档 Probe_Geometry_Columns()
给几何对象设置空间参考(在通过一个范围做空间查询时常用)ST_SetSRID(geometry, integer)

2.几何对象关系函数:
获取两个几何对象间的距离 ST_Distance(geometry, geometry)
如果两个几何对象间距离在给定值范围内,则返回TRUEST_DWithin(geometry, geometry, float)
判断两个几何对象是否相等
(比如LINESTRING(0 0, 2 2)和LINESTRING(0 0, 1 1, 2 2)是相同的几何对象)ST_Equals(geometry, geometry)
判断两个几何对象是否分离 ST_Disjoint(geometry, geometry)
判断两个几何对象是否相交 ST_Intersects(geometry, geometry)
判断两个几何对象的边缘是否接触 ST_Touches(geometry, geometry)
判断两个几何对象是否互相穿过 ST_Crosses(geometry, geometry)
判断A是否被B包含 ST_Within(geometry A, geometry B)
判断两个几何对象是否是重叠 ST_Overlaps(geometry, geometry)
判断A是否包含B ST_Contains(geometry A, geometry B)
判断A是否覆盖 B ST_Covers(geometry A, geometry B)
判断A是否被B所覆盖 ST_CoveredBy(geometry A, geometry B)
通过DE-9IM 矩阵判断两个几何对象的关系是否成立 ST_Relate(geometry, geometry, intersectionPatternMatrix)
获得两个几何对象的关系(DE-9IM矩阵) ST_Relate(geometry, geometry)

3.几何对象处理函数:
获取几何对象的中心 ST_Centroid(geometry)
面积量测 ST_Area(geometry)
长度量测 ST_Length(geometry)
返回曲面上的一个点 ST_PointOnSurface(geometry)
获取边界 ST_Boundary(geometry)
获取缓冲后的几何对象 ST_Buffer(geometry, double,[integer])
获取多几何对象的外接对象 ST_ConvexHull(geometry)
获取两个几何对象相交的部分 ST_Intersection(geometry, geometry)
将经度小于0的值加360使所有经度值在0-360间 ST_Shift_Longitude(geometry)
获取两个几何对象不相交的部分(A、B可互换) ST_SymDifference(geometry A,geometry B)
从A去除和B相交的部分后返回 ST_Difference(geometry A, geometryB)
返回两个几何对象的合并结果 ST_Union(geometry, geometry)
返回一系列几何对象的合并结果 ST_Union(geometry set)
用较少的内存和较长的时间完成合并操作,结果和ST_Union相同 ST_MemUnion(geometry set)

4.几何对象存取函数:
获取几何对象的WKT描述 ST_AsText(geometry)
获取几何对象的WKB描述 ST_AsBinary(geometry)
获取几何对象的空间参考ID ST_SRID(geometry)
获取几何对象的维数 ST_Dimension(geometry)
获取几何对象的边界范围 ST_Envelope(geometry)
判断几何对象是否为空 ST_IsEmpty(geometry)
判断几何对象是否不包含特殊点(比如自相交)ST_IsSimple(geometry)
判断几何对象是否闭合 ST_IsClosed(geometry)
判断曲线是否闭合并且不包含特殊点 ST_IsRing(geometry)
获取多几何对象中的对象个数 ST_NumGeometries(geometry)
获取多几何对象中第N个对象 ST_GeometryN(geometry,int)
获取几何对象中的点个数 ST_NumPoints(geometry)
获取几何对象的第N个点 ST_PointN(geometry,integer)
获取多边形的外边缘 ST_ExteriorRing(geometry)
获取多边形内边界个数 ST_NumInteriorRings(geometry)
同上 ST_NumInteriorRing(geometry)
获取多边形的第N个内边界 ST_InteriorRingN(geometry,integer)
获取线的终点 ST_EndPoint(geometry)
获取线的起始点 ST_StartPoint(geometry)
获取几何对象的类型 GeometryType(geometry)
类似上,但是不检查M值,即POINTM对象会被判断为point ST_GeometryType(geometry)
获取点的X坐标 ST_X(geometry)
获取点的Y坐标 ST_Y(geometry)
获取点的Z坐标 ST_Z(geometry)
获取点的M值 ST_M(geometry)

5.几何对象构造函数:
参考语义:
Text:WKT
WKB:WKB
Geom:Geometry
M:Multi
Bd:BuildArea
Coll:Collection ST_GeomFromText(text,[])    将wkt转为geometry

ST_PointFromText(text,[])
ST_LineFromText(text,[])
ST_LinestringFromText(text,[])
ST_PolyFromText(text,[])
ST_PolygonFromText(text,[])
ST_MPointFromText(text,[])
ST_MLineFromText(text,[])
ST_MPolyFromText(text,[])
ST_GeomCollFromText(text,[])
ST_GeomFromWKB(bytea,[])
ST_GeometryFromWKB(bytea,[])
ST_PointFromWKB(bytea,[])
ST_LineFromWKB(bytea,[])
ST_LinestringFromWKB(bytea,[])
ST_PolyFromWKB(bytea,[])
ST_PolygonFromWKB(bytea,[])
ST_MPointFromWKB(bytea,[])
ST_MLineFromWKB(bytea,[])
ST_MPolyFromWKB(bytea,[])
ST_GeomCollFromWKB(bytea,[])
ST_BdPolyFromText(text WKT, integer SRID)
ST_BdMPolyFromText(text WKT, integer SRID)

springboot工程 

springboot_mybatis_postgis: Springboot + mybatis + postgis 实现增删改查

Springboot + mybatis + postgis实现增删改查_postgis driver-class-name-CSDN博客

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jtstest.demo.mapper.ThreedPointsDao">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.jtstest.demo.entity.ThreedPoints">
        <result column="id" property="id" />
        <result column="location" property="location" />
        <result column="name" property="name" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
      id,location,name
    </sql>
    <select id="selectCustom" parameterType="com.jtstest.demo.dto.ThreedPointsQuery" resultType="com.jtstest.demo.entity.ThreedPoints">
        SELECT
        id,location,name
        FROM threed_points
        <where>
        <if test="name != null and name != ''">
            and  name = #{name,jdbcType=VARCHAR}
        </if>
        <if test="locationPositionStr != null and locationPositionStr != ''">
          and  ST_3DDistance(location, ST_GeomFromText(#{locationPositionStr,jdbcType=VARCHAR} , 4326))  <![CDATA[<=]]> 5
        </if>
        </where>
    </select>
</mapper>
package com.jtstest.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jtstest.demo.dto.ThreedPointsQuery;
import com.jtstest.demo.entity.ThreedPoints;
import org.springframework.stereotype.Component;
import java.util.List;


@Component
public interface ThreedPointsDao extends BaseMapper<ThreedPoints> {
    //
    public List<ThreedPoints> selectCustom(ThreedPointsQuery threedPoints);
}
package com.jtstest.demo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jtstest.demo.config.jts.JtsModule;
import com.jtstest.demo.dto.ThreedPointsQuery;
import com.jtstest.demo.entity.TGps;
import com.jtstest.demo.entity.ThreedPoints;
import com.jtstest.demo.mapper.TGpsDao;
import com.jtstest.demo.mapper.ThreedPointsDao;
import com.jtstest.demo.utils.JtsUtil;
import org.junit.jupiter.api.Test;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@SpringBootTest
public class DemoApplicationTests {
    @Autowired
    private TGpsDao tGpsDao;
    @Autowired
    private ThreedPointsDao threedPointsDao;

    private ObjectMapper objectMapper = new ObjectMapper().registerModule(new JtsModule());

    @Test
    void selectTest(){
        QueryWrapper<TGps> wrapper = new QueryWrapper<>();
//        wrapper.orderByAsc("time");
//        wrapper.last("limit 10");
        wrapper.eq("dev_id","abcdef");
        List<TGps> list = tGpsDao.selectList(wrapper);
        for (TGps entity : list) {
            System.out.println(entity);
        }
    }
   // @Test
    public void insertTest(){
        for(int i = 0 ;i<100000;i++) {
            TGps tGps = new TGps();
            tGps.setTime(new Date());
            tGps.setDevId("abcdef"+i);
            Random r = new Random();
            tGps.setLocation(JtsUtil.newPoint(Double.valueOf(r.nextInt(1000)), 34.0));
            tGps.setGpsNum(17);
            tGps.setGpsType("差分定位");
            tGps.setAzimuth(300.93);
            tGps.setGndRate(0.0);
            System.out.println(tGps);
            int insert = tGpsDao.insert(tGps);
            System.out.println("插入成功--> " + insert);
        }
    }
  //  @Test
    public void insertThreedPointsTest(){
        for(int i = 1000000 ;i<5000000;i++) {
            ThreedPoints tGps = new ThreedPoints();
            tGps.setId(Long.valueOf(i));
            tGps.setName("abcdef"+i);
            Random r = new Random();
            tGps.setLocation(JtsUtil.newPoint3d(Double.valueOf(r.nextInt(1000)), 34.0,Double.valueOf(r.nextInt(1000))));
            int insert = threedPointsDao.insert(tGps);
            System.out.println("插入成功--> " + insert);
        }
    }
    @Test
    void selectThreedPointsTest(){
        QueryWrapper<ThreedPoints> wrapper = new QueryWrapper<>();
//        wrapper.orderByAsc("time");
//        wrapper.last("limit 10");
        wrapper.eq("id",10);
        List<ThreedPoints> list = threedPointsDao.selectList(wrapper);
        for (ThreedPoints entity : list) {
            System.out.println(entity.getId()+"==========="+entity.getName()+"==========="+entity.getLocation().getCoordinate().getY());
        }
    }
    @Test
    void selectThreedPointsTest1(){
        ThreedPointsQuery threedPointsQuery = new ThreedPointsQuery();
       // threedPointsQuery.setName("abcdef1809552");
        System.out.println("selectThreedPointsTest1 =======");

        threedPointsQuery.setLocationPositionStr("POINTZ(23 34 0)");
        List<ThreedPoints> list = threedPointsDao.selectCustom(threedPointsQuery);
        for (ThreedPoints entity : list) {
            System.out.println("selectThreedPointsTest1 "+entity.getId()+"==========="+entity.getName()+"==========="+entity.getLocation().getCoordinate().getY());
        }
    }
    @Test
    void updateTest() {
        TGps tGps = new TGps();
        tGps.setTime(new Date());
        tGps.setDevId("abcdef");
        tGps.setLocation(JtsUtil.newPoint(113.0,34.0));
        tGps.setGpsNum(17);
        tGps.setGpsType("GPS定位");
        tGps.setAzimuth(300.93);
        tGps.setGndRate(0.0);
        QueryWrapper<TGps> wrapper = new QueryWrapper<>();
        wrapper.eq("dev_id","abcdef");
        int update = tGpsDao.update(tGps, wrapper);
        System.out.println("更新成功--> " + update);
    }
    @Test
    void deleteTest() {
        QueryWrapper<TGps> wrapper = new QueryWrapper<>();
        wrapper.eq("dev_id","abcdef");
      //  int delete = tGpsDao.delete(wrapper);
       // System.out.println("删除成功--> " + delete);
    }

    @Test
    void createGeometry() throws JsonProcessingException {
        /*
         * 创建点
         */
//        Point point = JtsUtil.newPoint(113.0,34.0);
//        System.out.println(point);
//        objectMapper.registerModule(new JtsModule());
//        System.out.println(objectMapper.writeValueAsString(point));

        /*
         * 创建线
         */
//        Point[] points = new Point[10];
//        for(int i = 0; i < 10; i ++){
//            points[i] = JtsUtil.newPoint(106.87+i, 27.53+i);
//        }
//        LineString lineString = JtsUtil.newLineString(points);
//        System.out.println(lineString);
//        System.out.println(objectMapper.writeValueAsString(lineString));

        /*
         * 根据查询结果创建线,数据库查询结果放在 points 中
         */
//        List<Point> points = new ArrayList<>();
//        for(int i = 0; i < 10; i ++){
//            points.add(JtsUtil.newPoint(106.87+i, 27.53+i));
//        }
//        points.add(JtsUtil.newPoint(106.87, 27.53));
//        Coordinate[] points1 = points.stream()
//                .map(Point::getCoordinate)
//                .toArray(Coordinate[]::new);
//        System.out.println(JtsUtil.geometryFactory4326.createLineString(points1));

        /*
         * 点创建面
         */
        List<Point> points = new ArrayList<>();
        for(int i = 0; i < 10; i ++){
            points.add(JtsUtil.newPoint(106.87+i, 27.53+i));
        }
        points.add(JtsUtil.newPoint(106.87, 27.53));
        Coordinate[] points1 = points.stream()
                .map(Point::getCoordinate)
                .toArray(Coordinate[]::new);
        Polygon polygon = JtsUtil.geometryFactory4326.createPolygon(points1);
        System.out.println(polygon);
        System.out.println(objectMapper.writeValueAsString(polygon));
    }

}

Create and manage spatial data with PostGIS Key Features Import and export geographic data from the PostGIS database using the available tools Maintain, optimize, and fine-tune spatial data for long-term viability Utilize the parallel support functionality that was introduced in PostgreSQL 9.6 Book Description PostGIS is a spatial database that integrates the advanced storage and analysis of vector and raster data, and is remarkably flexible and powerful. PostGIS provides support for geographic objects to the PostgreSQL object-relational database and is currently the most popular open source spatial databases. If you want to explore the complete range of PostGIS techniques and expose related extensions, then this book is for you. This book is a comprehensive guide to PostGIS tools and concepts which are required to manage, manipulate, and analyze spatial data in PostGIS. It covers key spatial data manipulation tasks, explaining not only how each task is performed, but also why. It provides practical guidance allowing you to safely take advantage of the advanced technology in PostGIS in order to simplify your spatial database administration tasks. Furthermore, you will learn to take advantage of basic and advanced vector, raster, and routing approaches along with the concepts of data maintenance, optimization, and performance, and will help you to integrate these into a large ecosystem of desktop and web tools. By the end, you will be armed with all the tools and instructions you need to both manage the spatial database system and make better decisions as your project's requirements evolve. What you will learn Import and export geographic data from the PostGIS database using the available tools Structure spatial data using the functionality provided by a combination of PostgreSQL and PostGIS Work with a set of PostGIS functions to perform basic and advanced vector analyses Connect PostGIS with Python Learn to use programming frameworks around PostGIS Maintain, optimize, and fine-tune spatial data for long-term viability Explore the 3D capabilities of PostGIS, including LiDAR point clouds and point clouds derived from Structure from Motion (SfM) techniques Distribute 3D models through the Web using the X3D standard Use PostGIS to develop powerful GIS web applications using Open Geospatial Consortium web standards Master PostGIS Raster Who This Book Is For This book is for developers who need some quick solutions for PostGIS. Prior knowledge of PostgreSQL and spatial concepts would be an added advantage. Table of Contents Chapter 1. MOVING DATA IN AND OUT OF POSTGIS Chapter 2. STRUCTURES THAT WORK Chapter 3. WORKING WITH VECTOR DATA THE BASICS Chapter 4. WORKING WITH VECTOR DATA ADVANCED RECIPES Chapter 5. WORKING WITH RASTER DATA Chapter 6. WORKING WITH PGROUTING Chapter 7. Into the Nth Dimension Chapter 8. POSTGIS PROGRAMMING Chapter 9. POSTGIS AND THE WEB Chapter 10. MAINTENANCE OPTIMIZATION AND PERFORMANCE TUNING Chapter 11. USING DESKTOP CLIENTS Chapter 12. Introduction to Location Privacy Protection Mechanisms
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值