在geoserver自定义的地图中通过geoserver wfs 查询,删除,添加相关的POI。
相关操作的格式如下:
查询
<wfs:GetFeature service="WFS" version="1.0.0"
outputFormat="GML2"
xmlns:opengis="http://www.cetusOpengis.com"
xmlns:wfs="http://www.opengis.net/wfs"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:gml="http://www.opengis.net/gml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">
<wfs:Query typeName="opengis:qqy">
<ogc:Filter>
<ogc:PropertyIsEqualTo>
<ogc:PropertyName>status</ogc:PropertyName>
<ogc:Literal>0</ogc:Literal>
</ogc:PropertyIsEqualTo>
</ogc:Filter>
</wfs:Query>
</wfs:GetFeature>;
添加
<wfs:Transaction service="WFS" version="1.0.0"
outputFormat="GML2"
xmlns:opengis="http://www.cetusOpengis.com"
xmlns:wfs="http://www.opengis.net/wfs"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:gml="http://www.opengis.net/gml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">
<wfs:Insert handle="someprj1">
<opengis:someprj>
<opengis:the_geom>
<gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#3395" >
<gml:coordinates decimal="." cs="," ts="">13404701.212,3850391.781</gml:coordinates>
</gml:Point>
</opengis:the_geom>
<opengis:ssds>13</opengis:ssds>
<opengis:qqybh>12</opengis:qqybh>
<opengis:status>0</opengis:status>
</opengis:someprj>
</wfs:Insert>
</wfs:Transaction>;
修改
<wfs:Transaction service="WFS" version="1.0.0"
outputFormat="GML2"
xmlns:opengis="http://www.cetusOpengis.com"
xmlns:wfs="http://www.opengis.net/wfs"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:gml="http://www.opengis.net/gml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">
<wfs:Update typeName="opengis:qqyproject">
<wfs:Property>
<wfs:Name>qqybh</wfs:Name>
<wfs:Value>12</wfs:Value>
</wfs:Property>
<ogc:Filter>
<ogc:PropertyIsEqualTo>
<ogc:PropertyName>qqybh</ogc:PropertyName>
<ogc:Literal>0</ogc:Literal>
</ogc:PropertyIsEqualTo>
</ogc:Filter>
</wfs:Update>
</wfs:Transaction>;
删除
<wfs:Transaction service="WFS" version="1.0.0"
outputFormat="GML2"
xmlns:opengis="http://www.cetusOpengis.com"
xmlns:wfs="http://www.opengis.net/wfs"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:gml="http://www.opengis.net/gml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">
<wfs:Delete typeName="opengis:qqyproject">
<ogc:Filter>
<ogc:PropertyIsLessThan>
<ogc:PropertyName>qqybh</ogc:PropertyName>
<ogc:Literal>12</ogc:Literal>
</ogc:PropertyIsLessThan>
<ogc:PropertyIsGreaterThan>
<ogc:PropertyName>qqybh</ogc:PropertyName>
<ogc:Literal>0</ogc:Literal>
</ogc:PropertyIsGreaterThan>
</ogc:Filter>
</wfs:Delete>
</wfs:Transaction>;
具体实现如下:
package com.geoserver; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; /** * 操作geoserver的几种方法 * * * * @Title: TODO * @Description: 实现TODO * @Copyright:Copyright (c) 2011 * @Company: * @Date:2012-8-30 * @author * @version 1.0 */ public class MainGeoService { public static void main(String[] args) { addGeoServerService(); } /** * 添加地图定的信息 */ public static void addGeoServerService(){ String layerName="loc_point"; String namespaceValue="http://www.easyway.net.cn"; double lat=139.54d; double lon=-116.23d; Map<String,String> params=new HashMap<String,String>(); params.put("FID", "loc_point.4"); params.put("NAME", "easyway_001"); params.put("OBJECT_CODE", "beijing_tsingperk_768"); params.put("HANDLE_ID", "768"); params.put("STATUS", "1"); params.put("DESCRIPTION", "this is point add by programe"); StringBuffer sb = new StringBuffer(); sb.append("<wfs:Transaction service='WFS' version='1.0.0' "); sb.append("outputFormat='GML2' "); sb.append(" xmlns:opengis='"+namespaceValue+"' "); sb.append(" xmlns:wfs='http://www.opengis.net/wfs' "); sb.append(" xmlns:ogc='http://www.opengis.net/ogc' "); sb.append(" xmlns:gml='http://www.opengis.net/gml' "); sb.append("xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "); sb.append(" xsi:schemaLocation='http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd'> "); sb.append(" <wfs:Insert handle='"+layerName+"'>"); sb.append(" <opengis:"+layerName+">"); sb.append(" <opengis:the_geom>"); sb.append(" <gml:Point srsName='http://www.opengis.net/gml/srs/epsg.xml#3395' >"); sb.append(" <gml:coordinates decimal='.' cs=',' ts=' '>"+lat+","+lon+"</gml:coordinates>"); sb.append(" </gml:Point>"); sb.append(" </opengis:the_geom>"); if(!params.isEmpty()){ for (Entry<String,String> entry : params.entrySet()) { sb.append(" <opengis:"+entry.getKey()+">"+entry.getValue()+"</opengis:"+entry.getKey()+">"); } } sb.append(" </opengis:"+layerName+">"); sb.append(" </wfs:Insert> "); sb.append(" </wfs:Transaction>"); String response = HttpUtils.doGeoServerPOST( "http://10.100.108.20:8080/geoserver/wfs?outputFormat=json", sb.toString()); System.out.println("response:"); System.out.println("" + response); } /** * 查询地图中的访问 * 假定我们指定的查询范围为bbox,根据上述参数设定,进行范围查询的Url为: * “WfsUrl?REQUEST=GetFeature&typeName= WfsNamespace : WfsLayerName &bbox=bbox&outputFormat=json”。 * bbox是怎么来的呢?bbox实际上描绘的是一个矩形,假定矩形左下角的点是Max(x1,y1),右上角的点是Min(x2,y2), * 则bbox是形如“x1,y1,x2,y2”的一个字符串。 */ public static void queryRangeGeoServerService() { //空间 String namespace="jacob"; //图层名称 String layerName="loc_point"; double minX=40.34d; double minY=65.344d; double maxX=45.34d; double maxY=67.34d; String queryRangeURL="http://10.100.108.20:8080/geoserver/wfs?REQUEST=GetFeature&typeName="+namespace+":"+layerName+"&bbox="+minX+","+minY+","+maxX+","+maxY+"&outputFormat=json"; String response = HttpUtils.doGeoServerPOST(queryRangeURL,""); System.out.println("response:"); System.out.println("" + response); } /** * 查询名称为cesuo 的地方 * */ public static void queryGeoServerService() { String namespace="jacob"; String namespaceValue="http://www.easyway.net.cn"; String layerName="loc_point"; Map<String,String> params=new HashMap<String,String>(); params.put("Name", "cesuo"); StringBuffer sb = new StringBuffer(); sb.append("<w:GetFeature service='WFS' version='1.1.0' "); sb.append(" xmlns:w='http://www.opengis.net/wfs' "); sb.append(" xmlns:f='"+namespaceValue+"' "); sb.append(" xmlns:g='http://www.opengis.net/gml' "); sb.append(" xmlns:o='http://www.opengis.net/ogc' "); sb.append(" x:schemaLocation='http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd WfsUrl/DescribeFeatureType?version=1.1.0&typename="+namespace+":"+layerName+"' "); sb.append(" xmlns:x='http://www.w3.org/2001/XMLSchema-instance'>"); sb.append(" <w:Query typeName='f:"+layerName+"'>"); sb.append(" <o:Filter>"); sb.append(" <o:PropertyIsEqualTo>"); if(!params.isEmpty()){ for (Entry<String,String> entry : params.entrySet()) { sb.append(" <o:PropertyName>f:"+entry.getKey()+"</o:PropertyName>"); sb.append(" <o:Literal>"+entry.getValue()+"</o:Literal>"); } } sb.append(" </o:PropertyIsEqualTo>"); sb.append(" </o:Filter>"); sb.append(" </w:Query>"); sb.append("</w:GetFeature>"); String response = HttpUtils.doGeoServerPOST( "http://10.100.108.20:8080/geoserver/wfs?outputFormat=json", sb.toString()); System.out.println("response:"); System.out.println("" + response); } /** * * 假定我们要删除的STATE_NAME为北京的点,则根据上述参数设定,此查询的url为:WfsUrl, * 同时需要将如下形式的参数信息,提交到服务器。如以post的方式, * 将参数信息写入HttpWebRequest的RequestStream中。 * */ public static void deletePointGeoServerService() { String namespace="jacob"; String namespaceValue="http://www.easyway.net.cn"; String layerName="loc_point"; Map<String,String> params=new HashMap<String,String>(); params.put("Name", "cesuo"); StringBuffer sb = new StringBuffer(); sb.append("<w:Transaction xmlns:w='http://www.opengis.net/wfs' "); sb.append(" xmlns:f='"+namespaceValue+"' xmlns:g='http://www.opengis.net/gml' "); sb.append(" service='WFS' version='1.1.0' xmlns:o='http://www.opengis.net/ogc' "); sb.append(" x:schemaLocation='http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd WfsUrl /DescribeFeatureType?version=1.1.0&typename="+namespace+":"+layerName+" ' xmlns:x='http://www.w3.org/2001/XMLSchema-instance'>"); sb.append(" <w:Delete typeName='f:"+layerName+"'>"); sb.append(" <o:Filter>"); sb.append(" <o:PropertyIsEqualTo>"); if(!params.isEmpty()){ for (Entry<String,String> entry : params.entrySet()) { sb.append(" <o:PropertyName>f:"+entry.getKey()+"</o:PropertyName>"); sb.append(" <o:Literal>"+entry.getValue()+"</o:Literal>"); } } sb.append(" </o:PropertyIsEqualTo>"); sb.append(" </o:Filter>"); sb.append(" </w:Delete>"); sb.append(" </w:Transaction>"); String response = HttpUtils.doGeoServerPOST( "http://10.100.108.20:8080/geoserver/wfs?outputFormat=json", sb.toString()); System.out.println("response:"); System.out.println("" + response); } }
package com.geoserver; import java.io.IOException; import java.io.ObjectInputStream; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @Title: TODO * @Description: 实现TODO * @Copyright:Copyright (c) 2011 * @Company: * @Date:2012-8-30 * @author * @version 1.0 */ public class HttpUtils { private final static Logger logger = LoggerFactory.getLogger(HttpUtils.class); private static final int DEFAULT_CONN_TIMEOUT_MILLISECONDS = 5 * 1000; private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = 60 * 1000; private static final String CHARSET = "utf-8"; private static HttpClient httpClient; static HttpHost proxy; static { proxy = new HttpHost("10.0.0.172", 80); } private static HttpClient initHttpClient(String charset) { if (charset == null) charset = System.getProperty("sun.jnu.encoding"); releaseConnection(); // 设置我们的HttpClient支持HTTP和HTTPS两种模式 SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); // 使用线程安全的连接管理来创建HttpClient /*ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(schemeRegistry); connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS); connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);*/ httpClient = new DefaultHttpClient(); /*HttpParams params = httpClient.getParams(); params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DEFAULT_CONN_TIMEOUT_MILLISECONDS); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, DEFAULT_READ_TIMEOUT_MILLISECONDS);*/ setConnectTimeout(DEFAULT_CONN_TIMEOUT_MILLISECONDS); setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS); return httpClient; } /** * HTTP post请求 * * @param url * 请求地址 * @param parmsMap * post数据 * @return */ public static String doGeoServerPOST(String url, String requst) { HttpPost httpPost = null; HttpResponse httpResponse = null; ObjectInputStream ois = null; try { if (httpClient == null) initHttpClient(CHARSET); httpPost = new HttpPost(url); // 绑定参数Entity StringEntity stringEntity = new StringEntity(requst, CHARSET); httpPost.setEntity(stringEntity); // 发送请求 httpResponse = httpClient.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() == 200) { Header[] headers = httpResponse.getAllHeaders(); for(Header h : headers){ System.err.println(h.getName() + " : " + h.getValue()); } HttpEntity entity = httpResponse.getEntity(); return EntityUtils.toString(entity); } return null; } catch (Exception e) { e.printStackTrace(); return null; } finally { try { if (ois != null) { ois.close(); } } catch (IOException e) { e.printStackTrace(); } httpClient.getConnectionManager().shutdown(); } } /** * Set the connection timeout for the underlying HttpClient. A timeout value * of 0 specifies an infinite timeout. * * @param timeout the timeout value in milliseconds */ private static void setConnectTimeout(int timeout) { httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); } /** * Set the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout * for waiting for data or, put differently, a maximum period inactivity * between two consecutive data packets.A timeout value of 0 specifies an * infinite timeout. * * @param timeout the timeout value in milliseconds */ private static void setReadTimeout(int timeout) { httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout); } private static void releaseConnection() { if (httpClient != null) httpClient.getConnectionManager().shutdown(); } }