(未完工)【GeoApp大型工程-Part 1】空间数据发布与管理 (Python + GeoServer + PostGIS + Postgresql + Tomcat)

1. Geoserver+Tomcat部署

1.1 安装Tomcat

1.Tomcat官网 下载8/9版本的Win安装包(10.x在部署Geoserver时会失败,原因未知,感兴趣的可自行尝试。)
2. 安装Tomcat,默认设置即可(安装最后一步可提前配置Tomcat的用户和密码,建议设置,后续也可手动设置)
3. (可选)若未在安装中修改用户信息,可打开/path/to/tomcat/conf/tomcat-user.xml文件,去掉一下代码的注释,并自行修改密码:
在这里插入图片描述4. 安装完成后,启动Tomcat客户端,点击Start按钮,启动服务,如下图:
在这里插入图片描述
6. 服务启动后,任意浏览器输入localhost:8080即可进入Tomcat管理页面
在这里插入图片描述

1.2 在Tomcat上部署Geoserver

  1. Geoserver官网下载对应版本的Web Archive文件(作者版本2.21.2 Stable)
  2. 解压下载的geoserver-2.21.2-war.zip文件
  3. 将解压的.war文件拷贝到/path/to/tomcat/webapps文件夹
  4. 点击Tomcat管理页面的Manager App选项,可在页面上看到geoserver选项
  5. 点击geoserver右侧的启动按钮,部署Geoserver
    在这里插入图片描述
  6. 打开/path/to/tomcat/webapps/geoserver/WEB-INF文件夹下的,web.xml文件,删除下列代码注释,开启Geoserver跨域(Tomcat下启动Geoserver跨域可能会导致无法启动服务,重新注释掉跨域内容即可):
   <!-- Uncomment following filter to enable CORS in Tomcat. Do not forget the second config block further down. -->
    <filter>
      <filter-name>cross-origin</filter-name>
      <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
      <init-param>
        <param-name>cors.allowed.origins</param-name>
        <param-value>*</param-value>
      </init-param>
      <init-param>
        <param-name>cors.allowed.methods</param-name>
        <param-value>GET,POST,PUT,DELETE,HEAD,OPTIONS</param-value>
      </init-param>
      <init-param>
        <param-name>cors.allowed.headers</param-name>
        <param-value>*</param-value>
      </init-param>
    </filter>
   <!-- Uncomment following filter to enable CORS -->
    <filter-mapping>
        <filter-name>cross-origin</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
   

1.3 (可选)使用花生壳实现外网访问Geoserver

  1. (可选)花生壳内网穿透步骤
  2. 下载花生壳客户端,创建用户后,添加内网映射
  3. 选择http,内网位置填入局域网Ip,如192.168.1.21,端口默认8080(可根据实际使用修改)
  4. 创建完成后,会自动分配访问域名,在电脑输入即可实现访问。
  1. 若不使用花生壳等映射软件,需要打开/path/to/tomcat/conf/serverl.xml文件,用电脑的公网IP替换文件中的的两处“localhost”(注:公网IP可直接百度输入IP查询),后续配置局域网端口映射可自定百度。
   <!-- <Engine name="Catalina" defaultHost="localhost">  -->
   <Engine name="Catalina" defaultHost="111.111.111.111"> 
      <!-- <Host name="localhost"  appBase="webapps" -->
      <Host name="111.11.111.111"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

2. Geoserver REST API 代码示例

2.1 栅格数据操作
2.1.1 获取当前Workspace/Stores/Coverages列表
auth    = ('admin', 'geoserver')
headers = {'Content-type': 'text/plain'}
url     = "http://localhost:8088/geoserver/rest/workspaces" 
r       = requests.get(url=url, auth=auth, headers=headers)
print('Existed Workspaces:',r.json())
url     = "http://localhost:8088/geoserver/rest/coveragestores"
r       = requests.get(url=url, auth=auth, headers=headers)
print('Existed Coveragestores:',r.json())
url     = "http://localhost:8088/geoserver/rest/coverages"
r       = requests.get(url=url, auth=auth, headers=headers)
print('Existed Coverages:',r.json())
2.1.2 创建Workspace/Coveragestore/Coverage(发布图层)

1-Workspace(工作区)

workspaceName = 'your_workspace_name'
r = requests.post(url = url, 
				  auth = auth, 
				  headers = {'Content-type': 'text/xml'}, 
				  data = f"<workspace><name>{workspaceName}</name></workspace>")

2 - Coveragestore(仓库)


coveragestoreName = 'your_coveragestore_name'
xml_content = f"""
	<coverageStore>
		<name>{coveragestoreName}</name>
		<description>Query Result</description>
		<type>GeoTIFF</type>
		<enabled>true</enabled>
		<url>file:data/{workspaceName}/custom_layer.tif</url>
		<workspace>
			<name>{workspaceName}</name>
			<link>http://localhost:8080/geoserver/rest/workspaces/{workspaceName}</link>
		</workspace>
	</coverageStore>"""

r = requests.post(url = url, 
                  auth = auth, 
                  headers = {'accept':'application/json','Content-type': 'application/xml'}, 
                  data = xml_content)
          

3-Coverage(图层)

coverageName= 'your_coverage_name'
url = f"http://localhost:8080/geoserver/rest/workspaces/{workspaceName}/coveragestores/{coveragestoreName}/coverages"
json_content = {
"coverage": {
    "name": coverageName,
    "enabled": True,
    "store": {
    "@class": "coverageStore",
    "href": f"http://localhost:8080/geoserver/restng/workspaces/{workspaceName}/coveragestores/{coveragestoreName}.json",
    "name":  f"{workspaceName}:{coveragestoreName}"
    },
    "title": "Your Coverage Title"
}
}
r = requests.post(url = url, 
                  auth = auth, 
                  headers = {'accept':'application/json','Content-type': 'application/json'}, 
                  data = json.dumps(json_content))

3、PostgreSQL和PostGIS Administration安装与使用

3.1 安装Postgresql(注意版本选择,教程版本为14)

3.2 安装PostGIS Administration (教程版本:pgAdmin 14)

3.3 Postgres数据操作

3.3.1 添加空间数据扩展(矢量和栅格)
# Connecting Postgres with user & pwd
postgres -h localhost -p 5432 -U postgres # 台式机code:zhangyz@128

# PgAdmin-Tools:
#-- 矢量postgis
create extension postgis;
#-- 栅格postgis
create extension postgis_raster;

# 其他Extensions(可选)
create extension postgis_topology # geometry属性
create extension addresss_standardizer
create extension postgis_trigger_geocoder
create extension fuzzystrmatch

3.3.2 矢量数据操作

3.3.2.1 数据导入(Import)
3.3.2.2 数据查询(Querry)
"""使用矢量进行裁剪导出"""
# postgresql+psycopg2 或 mysql+pymysql 
engine = create_engine(
    f"postgresql+psycopg2://{user}:{urlquote(password)}@{host}:{port}/{database}")
connection = engine.connect()

sql = " SELECT rast, geom \
        FROM daily_rainfall, (SELECT * FROM guangxi_cities WHERE name_2 = 'Nanning') foo\
        WHERE ST_Clip(rast, geom,0,true)"
connection.execute(sql)

3.1.4 栅格数据操作

3.1.4.1 数据导入
"""利用raster2pgsql命令,批量导入文件夹的.tif文件。"""
# CREATE TABLE myrasters(rid serial primary key, rast raster);
raster2pgsql -s 4326 -I -C -M *.tif public.myrasters| psql -d db_name -U postgres

3.3

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值