ใช้งาน Web Processing Service (WPS) บน Geoserver ด้วย Geojson

Geoserver คือ ?
เซิฟเวอร์ที่พัฒนาในการจัดการและคำนวณ ภูมิสารสนเทศออกแบบมาเพื่อการทำงานร่วมกัน โดยเผยแพร่ข้อมูลจากแหล่งข้อมูลเชิงพื้นที่สำคัญใด ๆ โดยใช้มาตรฐานแบบเปิด Geoserver เป็น Open source ที่เขียนด้วยภาษา Java มีการทำงานด้วย JVM Runtime โดยเหมาะสำหรับทีมพัฒนาทั้ง Developer และ Operator
WPS คือ ?
Web Processing Service เป็นบริการ OGC สำหรับการเผยแพร่กระบวนการเชิงพื้นที่ อัลกอริทึม และการคำนวณสำหรับ Geoserver ที่ให้การดำเนินการประมวลผลข้อมูลและการวิเคราะห์เชิงพื้นที่
Geojson คือ ?
โครงสร้างการเข้ารหัสของภูมิศาสตร์ที่หลากหลาย โดยรองรับประเภทเรขาคณิตได้แก่ Point
, LineString
, Polygon
, MultiPoint
, MultiLineString
, MultiPolygon
โดยในปี 2015 Internet Engineering Task Force (IETF) ร่วมกับผู้เขียนข้อมูลจำเพาะดั้งเดิม ได้จัดตั้ง Geojson WG ขึ้น
จุดประสงค์ของหัวข้อนี้
เราต้องการใช้ WPS ของ Geoserver ในการทำ RasterZonalStatistics โดยใช้ Geojson เป็นตัว Intersection กับข้อมูล Raster
ขั้นตอนการเตรียมตัว
อย่างแรกสิ่งที่จำเป็นจะต้องเตรียมความพร้อมและต้องมีได้แก่
- Geoserver ได้ทั้ง Container และ VMWare
- ข้อมูล ไม่ว่าจะเป็น geopackage, geotiff ก็ได้
- ข้อมูล Geojson
- ความรู้ XML
ประกาศการใช้งาน XML
<?xml version="1.0" encoding="UTF-8"?>
ประกาศ WPS Execute แบบ XML
<?xml version="1.0" encoding="UTF-8"?>
<wps:Execute version="1.0.0" service="WPS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wps/1.0.0" xmlns:wfs="http://www.opengis.net/wfs" xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:wcs="http://www.opengis.net/wcs/1.1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd">
</wps:Execute>
ประกาศ OWS และเรียก Identifier RasterZonalStatistics
เป็นบริการที่เอาไว้ติดต่อ Geoserver โดยมีหลากหลายประเภท WCS , WFS , WMS ซึ่งเราจะเรียกบริการโดยรวมว่า OWS
<?xml version="1.0" encoding="UTF-8"?>
<wps:Execute version="1.0.0" service="WPS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wps/1.0.0" xmlns:wfs="http://www.opengis.net/wfs" xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:wcs="http://www.opengis.net/wcs/1.1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd">
<ows:Identifier>ras:RasterZonalStatistics</ows:Identifier>
</wps:Execute>
ประกาศ WPS Data Inputs
โดย ows BuondingBox เป็นจุดแกน X and Y ของแผนที่ภูมิศาสตร์ ซึ่งในตัวอย่างที่ใส่ลงไปนี้คือ ประเทศไทย และ EPSG ตั้งตามที่เราใช้ได้เลย
จุดที่ให้เราระบุเพิ่มเติมเปลี่ยนแปลงได้
- worksapce
- layer
- epsg_number
- band_number
<?xml version="1.0" encoding="UTF-8"?>
<wps:Execute version="1.0.0" service="WPS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wps/1.0.0" xmlns:wfs="http://www.opengis.net/wfs" xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:wcs="http://www.opengis.net/wcs/1.1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd">
<ows:Identifier>ras:RasterZonalStatistics</ows:Identifier>
<wps:DataInputs>
<wps:Input>
<ows:Identifier>data</ows:Identifier>
<wps:Reference mimeType="image/tiff" xlink:href="http://geoserver/wcs" method="POST">
<wps:Body>
<wcs:GetCoverage service="WCS" version="1.1.1">
<ows:Identifier>{workspace}:{layer}</ows:Identifier>
<wcs:DomainSubset>
<ows:BoundingBox crs="http://www.opengis.net/gml/srs/epsg.xml#{epsg_number}">
<ows:LowerCorner>97.3758964376 5.69138418215</ows:LowerCorner>
<ows:UpperCorner>105.589038527 20.4178496363</ows:UpperCorner>
</ows:BoundingBox>
</wcs:DomainSubset>
<wcs:Output format="image/tiff"/>
</wcs:GetCoverage>
</wps:Body>
</wps:Reference>
</wps:Input>
<wps:Input>
<ows:Identifier>band</ows:Identifier>
<wps:Data>
<wps:LiteralData>{band_number}</wps:LiteralData>
</wps:Data>
</wps:Input>
<wps:Input>
<ows:Identifier>zones</ows:Identifier>
<wps:Data>
</wps:Data>
</wps:Input>
</wps:DataInputs>
</wps:Execute>
ประกาศ WPS ComplexData
กำหนด mimeType ให้รองรับกับ json และวาง Geojson ตามที่ระบุไว้ได้เลย
จุดที่ให้เราระบุเพิ่มเติมเปลี่ยนแปลงได้
- geojson
<?xml version="1.0" encoding="UTF-8"?>
<wps:Execute version="1.0.0" service="WPS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wps/1.0.0" xmlns:wfs="http://www.opengis.net/wfs" xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:wcs="http://www.opengis.net/wcs/1.1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd">
<ows:Identifier>ras:RasterZonalStatistics</ows:Identifier>
<wps:DataInputs>
<wps:Input>
<ows:Identifier>data</ows:Identifier>
<wps:Reference mimeType="image/tiff" xlink:href="http://geoserver/wcs" method="POST">
<wps:Body>
<wcs:GetCoverage service="WCS" version="1.1.1">
<ows:Identifier>{workspace}:{layer}</ows:Identifier>
<wcs:DomainSubset>
<ows:BoundingBox crs="http://www.opengis.net/gml/srs/epsg.xml#{epsg_number}">
<ows:LowerCorner>97.3758964376 5.69138418215</ows:LowerCorner>
<ows:UpperCorner>105.589038527 20.4178496363</ows:UpperCorner>
</ows:BoundingBox>
</wcs:DomainSubset>
<wcs:Output format="image/tiff"/>
</wcs:GetCoverage>
</wps:Body>
</wps:Reference>
</wps:Input>
<wps:Input>
<ows:Identifier>band</ows:Identifier>
<wps:Data>
<wps:LiteralData>{band_number}</wps:LiteralData>
</wps:Data>
</wps:Input>
<wps:Input>
<ows:Identifier>zones</ows:Identifier>
<wps:Data>
<wps:ComplexData mimeType="application/json"><![CDATA[{geojson}]]></wps:ComplexData>
</wps:Data>
</wps:Input>
</wps:DataInputs>
</wps:Execute>
ประกาศ WPS ResponseForm
เมื่อเราส่งข้อมูลเข้าแล้วเราก็ควรจะต้องส่งข้อมูลออกมาด้วย โดยเราจะกำหนดให้แสดงข้อมูลเป็น mimeType Json
<?xml version="1.0" encoding="UTF-8"?>
<wps:Execute version="1.0.0" service="WPS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wps/1.0.0" xmlns:wfs="http://www.opengis.net/wfs" xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:wcs="http://www.opengis.net/wcs/1.1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd">
<ows:Identifier>ras:RasterZonalStatistics</ows:Identifier>
<wps:DataInputs>
<wps:Input>
<ows:Identifier>data</ows:Identifier>
<wps:Reference mimeType="image/tiff" xlink:href="http://geoserver/wcs" method="POST">
<wps:Body>
<wcs:GetCoverage service="WCS" version="1.1.1">
<ows:Identifier>{workspace}:{layer}</ows:Identifier>
<wcs:DomainSubset>
<ows:BoundingBox crs="http://www.opengis.net/gml/srs/epsg.xml#4326">
<ows:LowerCorner>97.3758964376 5.69138418215</ows:LowerCorner>
<ows:UpperCorner>105.589038527 20.4178496363</ows:UpperCorner>
</ows:BoundingBox>
</wcs:DomainSubset>
<wcs:Output format="image/tiff"/>
</wcs:GetCoverage>
</wps:Body>
</wps:Reference>
</wps:Input>
<wps:Input>
<ows:Identifier>band</ows:Identifier>
<wps:Data>
<wps:LiteralData>0</wps:LiteralData>
</wps:Data>
</wps:Input>
<wps:Input>
<ows:Identifier>zones</ows:Identifier>
<wps:Data>
<wps:ComplexData mimeType="application/json"><![CDATA[{geojson}]]></wps:ComplexData>
</wps:Data>
</wps:Input>
</wps:DataInputs>
<wps:ResponseForm>
<wps:RawDataOutput mimeType="application/json">
<ows:Identifier>statistics</ows:Identifier>
</wps:RawDataOutput>
</wps:ResponseForm>
</wps:Execute>
ทำการส่งข้อมูลผ่าน Rest api
curl -X POST 'https://XXXXXXXX.com/geoserver/wps' --header 'Content-Type: application/xml' --data-row '{XML}'
ผลลัพธ์เมื่อได้ข้อมูล โดยข้อมูลจะถูกส่งคืนใน properties ตามที่เราสร้างไว้
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"min": 1.000000,
"max": 1.000000
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
103.11083,
15.176361
],
[
103.06933976,
15.08824751
],
[
103.16684183,
15.02881701
],
[
103.2304602,
15.11762274
],
[
103.16338431,
15.17035443
],
[
103.11083,
15.17836316
],
[
103.11152151,
15.17502622
],
[
103.05343517,
15.12830364
],
[
103.05481817,
15.08757984
],
[
103.07210577,
15.08891517
],
[
103.11083,
15.176361
]
]
]
},
"id": "features.0"
}
]
}
ต้องขอเล่าว่า Blog Geoserver แทบจะไม่สามารถเรียนรู้ได้เท่าที่ควร ส่วนใหญ่เกิดจากประสบการณ์รวมถึงความรู้ที่ลองผิดลองถูก 🤦
หากท่านชอบและรู้สึกว่ามีประโยชน์ รบกวนแชร์และแบ่งปันให้ผู้ที่สนใจและควรจะได้รับการเข้าถึงบทความนี้ แล้วพบกันใหม่ครับ