【GIS】PostGIS使用案例

近期在使用PostGIS处理数据时,需要按照矢量范围对栅格数据进行统计分析,但是在默认的数据库函数中没有现成的函数,因此自己简单写了一个函数,用于数据分析,代码如下所示:

-- 栅格数据按照范围统计
create or replace function public.zonal_statistics(
	rast raster,
	geom geometry,
	nband integer default 1
)
returns summarystats
as $$
declare 
	stats summarystats;
begin
	-- 判断坐标是否一致
	if ST_SRID(rast) == ST_SRID(geom) then
		with clip_raster as (
			select ST_Clip(rast,nband,geom,ST_MinPossibleValue(ST_BandPixelType(rast, nband))) as ras 
			where ST_Intersects(geom,raster)
		)
		select (ST_SummaryStats(r.ras)).* into stats from clip_raster as r;
	else
		raise EXCEPTION 'geom srid is not equal to raster srid!';
		return null;
	end if;
	return stats;
end;
$$
language 'plpgsql'
immutable
strict
parallel safe;

comment on function public.zonal_statistics is '栅格数据按照区域统计(总数、最大值、最小值、标准差、平均值)';
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PostGIS is an open-source extension for the PostgreSQL database management system that allows you to store and manipulate spatial data. Here are some steps to get started with using PostGIS: 1. Install PostgreSQL and PostGIS: Before you can start using PostGIS, you need to have PostgreSQL installed on your system. You can download the latest version of PostgreSQL from the official website. Once you have installed PostgreSQL, you can install the PostGIS extension by running the following command in the terminal: ``` CREATE EXTENSION postgis; ``` 2. Create a database with PostGIS support: To create a database with PostGIS support, you need to connect to the PostgreSQL server and run the following SQL statement: ``` CREATE DATABASE mydb WITH TEMPLATE template_postgis; ``` 3. Load spatial data into the database: Once you have created a database with PostGIS support, you can load spatial data into the database. You can do this by using the `ogr2ogr` command-line tool or by using a graphical tool such as QGIS or pgAdmin. 4. Query the spatial data: You can query the spatial data stored in the PostGIS database by using SQL statements. For example, to find all the points within a certain distance of a given point, you can use the following SQL statement: ``` SELECT * FROM mytable WHERE ST_DWithin(the_geom, ST_GeomFromText('POINT(longitude latitude)', 4326), distance); ``` 5. Visualize the spatial data: You can visualize the spatial data stored in the PostGIS database by using a graphical tool such as QGIS or Mapbox Studio. These are the basic steps to get started with using PostGIS. To learn more about using PostGIS, you can refer to the official documentation and other online resources.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值