GIS的学习(十九)Geoserver使用添加,删除,查询地图中的POI

来源:互联网 发布:怎么查看数据库关系 编辑:程序博客网 时间:2024/05/18 03:13

GIS的学习(十九)Geoserver使用添加,删除,查询地图中的POI

   在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>;

 

 

具体实现如下:

Java代码  收藏代码
  1. package com.geoserver;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5. import java.util.Map.Entry;  
  6.   
  7. /** 
  8.  * 操作geoserver的几种方法 
  9.  *  
  10.  *  
  11.  *  
  12.  * @Title: TODO 
  13.  * @Description: 实现TODO 
  14.  * @Copyright:Copyright (c) 2011 
  15.  * @Company: 
  16.  * @Date:2012-8-30 
  17.  * @author  
  18.  * @version 1.0 
  19.  */  
  20. public class MainGeoService {  
  21.   
  22.     public static void main(String[] args) {  
  23.         addGeoServerService();  
  24.           
  25.       
  26.     }  
  27.     /** 
  28.      * 添加地图定的信息 
  29.      */  
  30.     public static void addGeoServerService(){  
  31.         String layerName="loc_point";  
  32.         String namespaceValue="http://www.easyway.net.cn";  
  33.         double lat=139.54d;  
  34.         double lon=-116.23d;  
  35.           
  36.          Map<String,String> params=new HashMap<String,String>();  
  37.          params.put("FID""loc_point.4");  
  38.          params.put("NAME""easyway_001");  
  39.          params.put("OBJECT_CODE""beijing_tsingperk_768");  
  40.          params.put("HANDLE_ID""768");  
  41.          params.put("STATUS""1");  
  42.          params.put("DESCRIPTION""this is point add by programe");  
  43.               
  44.            
  45.         StringBuffer sb = new StringBuffer();  
  46.         sb.append("<wfs:Transaction service='WFS' version='1.0.0'   ");  
  47.         sb.append("outputFormat='GML2'   ");  
  48.         sb.append(" xmlns:opengis='"+namespaceValue+"'   ");  
  49.         sb.append(" xmlns:wfs='http://www.opengis.net/wfs'   ");  
  50.         sb.append(" xmlns:ogc='http://www.opengis.net/ogc'   ");  
  51.         sb.append(" xmlns:gml='http://www.opengis.net/gml'   ");  
  52.         sb.append("xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'   ");  
  53.         sb.append(" xsi:schemaLocation='http://www.opengis.net/wfs   http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd'>   ");   
  54.         sb.append(" <wfs:Insert handle='"+layerName+"'>");  
  55.         sb.append("  <opengis:"+layerName+">");  
  56.         sb.append("    <opengis:the_geom>");  
  57.         sb.append("     <gml:Point srsName='http://www.opengis.net/gml/srs/epsg.xml#3395' >");  
  58.         sb.append("     <gml:coordinates decimal='.' cs=',' ts=' '>"+lat+","+lon+"</gml:coordinates>");  
  59.         sb.append("   </gml:Point>");  
  60.         sb.append(" </opengis:the_geom>");  
  61.           
  62.         if(!params.isEmpty()){  
  63.             for (Entry<String,String> entry : params.entrySet()) {  
  64.                 sb.append("      <opengis:"+entry.getKey()+">"+entry.getValue()+"</opengis:"+entry.getKey()+">");  
  65.             }  
  66.         }  
  67.         sb.append("  </opengis:"+layerName+">");  
  68.         sb.append(" </wfs:Insert>   ");  
  69.         sb.append(" </wfs:Transaction>");  
  70.         String response = HttpUtils.doGeoServerPOST(  
  71.                 "http://10.100.108.20:8080/geoserver/wfs?outputFormat=json",  
  72.                 sb.toString());  
  73.         System.out.println("response:");  
  74.         System.out.println("" + response);  
  75.     }  
  76.     /** 
  77.      * 查询地图中的访问 
  78.      * 假定我们指定的查询范围为bbox,根据上述参数设定,进行范围查询的Url为: 
  79.      * “WfsUrl?REQUEST=GetFeature&typeName= WfsNamespace : WfsLayerName &bbox=bbox&outputFormat=json”。 
  80.      * bbox是怎么来的呢?bbox实际上描绘的是一个矩形,假定矩形左下角的点是Max(x1,y1),右上角的点是Min(x2,y2), 
  81.      * 则bbox是形如“x1,y1,x2,y2”的一个字符串。 
  82.      */  
  83.     public static void queryRangeGeoServerService() {  
  84.         //空间   
  85.         String namespace="jacob";  
  86.         //图层名称  
  87.         String layerName="loc_point";  
  88.            
  89.         double minX=40.34d;  
  90.         double minY=65.344d;  
  91.         double maxX=45.34d;  
  92.         double maxY=67.34d;  
  93.            
  94.         String queryRangeURL="http://10.100.108.20:8080/geoserver/wfs?REQUEST=GetFeature&typeName="+namespace+":"+layerName+"&bbox="+minX+","+minY+","+maxX+","+maxY+"&outputFormat=json";  
  95.         String response = HttpUtils.doGeoServerPOST(queryRangeURL,"");  
  96.         System.out.println("response:");  
  97.         System.out.println("" + response);  
  98.     }  
  99.     /** 
  100.      * 查询名称为cesuo 的地方 
  101.      *  
  102.      */  
  103.     public static void queryGeoServerService() {  
  104.          String namespace="jacob";  
  105.          String namespaceValue="http://www.easyway.net.cn";  
  106.          String layerName="loc_point";  
  107.          Map<String,String> params=new HashMap<String,String>();  
  108.          params.put("Name""cesuo");  
  109.            
  110.         StringBuffer sb = new StringBuffer();  
  111.         sb.append("<w:GetFeature service='WFS' version='1.1.0' ");  
  112.         sb.append(" xmlns:w='http://www.opengis.net/wfs' ");  
  113.         sb.append(" xmlns:f='"+namespaceValue+"' ");  
  114.         sb.append(" xmlns:g='http://www.opengis.net/gml' ");  
  115.         sb.append(" xmlns:o='http://www.opengis.net/ogc' ");  
  116.         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&amp;typename="+namespace+":"+layerName+"' ");  
  117.         sb.append(" xmlns:x='http://www.w3.org/2001/XMLSchema-instance'>");  
  118.         sb.append(" <w:Query typeName='f:"+layerName+"'>");  
  119.         sb.append("  <o:Filter>");  
  120.         sb.append("     <o:PropertyIsEqualTo>");  
  121.         if(!params.isEmpty()){  
  122.             for (Entry<String,String> entry : params.entrySet()) {  
  123.                 sb.append("      <o:PropertyName>f:"+entry.getKey()+"</o:PropertyName>");  
  124.                 sb.append("       <o:Literal>"+entry.getValue()+"</o:Literal>");  
  125.             }  
  126.         }  
  127.         sb.append("   </o:PropertyIsEqualTo>");  
  128.         sb.append(" </o:Filter>");  
  129.         sb.append(" </w:Query>");  
  130.         sb.append("</w:GetFeature>");  
  131.         String response = HttpUtils.doGeoServerPOST(  
  132.                 "http://10.100.108.20:8080/geoserver/wfs?outputFormat=json",  
  133.                 sb.toString());  
  134.         System.out.println("response:");  
  135.         System.out.println("" + response);  
  136.     }  
  137.   
  138.     /** 
  139.      *  
  140.      * 假定我们要删除的STATE_NAME为北京的点,则根据上述参数设定,此查询的url为:WfsUrl, 
  141.      * 同时需要将如下形式的参数信息,提交到服务器。如以post的方式, 
  142.      * 将参数信息写入HttpWebRequest的RequestStream中。 
  143.      *  
  144.      */  
  145.     public static void deletePointGeoServerService() {  
  146.           
  147.          String namespace="jacob";  
  148.          String namespaceValue="http://www.easyway.net.cn";  
  149.          String layerName="loc_point";  
  150.          Map<String,String> params=new HashMap<String,String>();  
  151.          params.put("Name""cesuo");  
  152.           
  153.         StringBuffer sb = new StringBuffer();  
  154.         sb.append("<w:Transaction xmlns:w='http://www.opengis.net/wfs' ");  
  155.         sb.append(" xmlns:f='"+namespaceValue+"' xmlns:g='http://www.opengis.net/gml' ");  
  156.         sb.append("  service='WFS' version='1.1.0' xmlns:o='http://www.opengis.net/ogc' ");  
  157.         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&amp;typename="+namespace+":"+layerName+" ' xmlns:x='http://www.w3.org/2001/XMLSchema-instance'>");  
  158.         sb.append("   <w:Delete typeName='f:"+layerName+"'>");  
  159.         sb.append("  <o:Filter>");  
  160.         sb.append("    <o:PropertyIsEqualTo>");  
  161.         if(!params.isEmpty()){  
  162.             for (Entry<String,String> entry : params.entrySet()) {  
  163.                 sb.append("      <o:PropertyName>f:"+entry.getKey()+"</o:PropertyName>");  
  164.                 sb.append("       <o:Literal>"+entry.getValue()+"</o:Literal>");  
  165.             }  
  166.         }  
  167.         sb.append("   </o:PropertyIsEqualTo>");  
  168.         sb.append("        </o:Filter>");  
  169.         sb.append("     </w:Delete>");  
  170.         sb.append(" </w:Transaction>");  
  171.         String response = HttpUtils.doGeoServerPOST(  
  172.                 "http://10.100.108.20:8080/geoserver/wfs?outputFormat=json",  
  173.                 sb.toString());  
  174.         System.out.println("response:");  
  175.         System.out.println("" + response);  
  176.     }  
  177. }  

 

http工具类:
Java代码  收藏代码
  1. package com.geoserver;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.ObjectInputStream;  
  5.   
  6. import org.apache.http.Header;  
  7. import org.apache.http.HttpEntity;  
  8. import org.apache.http.HttpHost;  
  9. import org.apache.http.HttpResponse;  
  10. import org.apache.http.client.HttpClient;  
  11. import org.apache.http.client.methods.HttpPost;  
  12. import org.apache.http.conn.scheme.PlainSocketFactory;  
  13. import org.apache.http.conn.scheme.Scheme;  
  14. import org.apache.http.conn.scheme.SchemeRegistry;  
  15. import org.apache.http.conn.ssl.SSLSocketFactory;  
  16. import org.apache.http.entity.StringEntity;  
  17. import org.apache.http.impl.client.DefaultHttpClient;  
  18. import org.apache.http.params.CoreConnectionPNames;  
  19. import org.apache.http.util.EntityUtils;  
  20. import org.slf4j.Logger;  
  21. import org.slf4j.LoggerFactory;  
  22.   
  23. /** 
  24.  *  
  25.  * @Title: TODO 
  26.  * @Description: 实现TODO 
  27.  * @Copyright:Copyright (c) 2011 
  28.  * @Company: 
  29.  * @Date:2012-8-30 
  30.  * @author  
  31.  * @version 1.0 
  32.  */  
  33. public class HttpUtils {  
  34.   
  35.     private final static Logger logger = LoggerFactory.getLogger(HttpUtils.class);  
  36.       
  37.   
  38.     private static final int    DEFAULT_CONN_TIMEOUT_MILLISECONDS   = 5 * 1000;  
  39.   
  40.     private static final int    DEFAULT_READ_TIMEOUT_MILLISECONDS   = 60 * 1000;  
  41.   
  42.   
  43.     private static final String CHARSET = "utf-8";  
  44.       
  45.     private static HttpClient httpClient;  
  46.       
  47.     static HttpHost proxy;  
  48.   
  49.     static {  
  50.         proxy = new HttpHost("10.0.0.172"80);  
  51.     }  
  52.       
  53.     private static HttpClient initHttpClient(String charset) {  
  54.         if (charset == null)  
  55.             charset = System.getProperty("sun.jnu.encoding");  
  56.         releaseConnection();  
  57.           
  58.         // 设置我们的HttpClient支持HTTP和HTTPS两种模式  
  59.         SchemeRegistry schemeRegistry = new SchemeRegistry();  
  60.         schemeRegistry.register(new Scheme("http"80, PlainSocketFactory.getSocketFactory()));  
  61.         schemeRegistry.register(new Scheme("https"443, SSLSocketFactory.getSocketFactory()));  
  62.   
  63.         // 使用线程安全的连接管理来创建HttpClient  
  64.         /*ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(schemeRegistry); 
  65.         connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS); 
  66.         connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);*/  
  67.         httpClient = new DefaultHttpClient();  
  68.   
  69.         /*HttpParams params = httpClient.getParams(); 
  70.         params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 
  71.                 DEFAULT_CONN_TIMEOUT_MILLISECONDS); 
  72.         params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 
  73.                 DEFAULT_READ_TIMEOUT_MILLISECONDS);*/  
  74.           
  75.         setConnectTimeout(DEFAULT_CONN_TIMEOUT_MILLISECONDS);  
  76.         setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);  
  77.           
  78.         return httpClient;  
  79.     }  
  80.       
  81.       
  82.   
  83.   
  84.   
  85.     /** 
  86.      * HTTP post请求 
  87.      *  
  88.      * @param url 
  89.      *            请求地址 
  90.      * @param parmsMap 
  91.      *            post数据 
  92.      * @return 
  93.      */  
  94.     public static String doGeoServerPOST(String url, String requst) {  
  95.         HttpPost httpPost = null;  
  96.         HttpResponse httpResponse = null;  
  97.         ObjectInputStream ois = null;  
  98.         try {  
  99.             if (httpClient == null)  
  100.                 initHttpClient(CHARSET);  
  101.               
  102.             httpPost = new HttpPost(url);  
  103.             // 绑定参数Entity  
  104.             StringEntity stringEntity = new StringEntity(requst, CHARSET);  
  105.             httpPost.setEntity(stringEntity);  
  106.             // 发送请求  
  107.             httpResponse = httpClient.execute(httpPost);  
  108.   
  109.             if (httpResponse.getStatusLine().getStatusCode() == 200) {  
  110.                 Header[] headers = httpResponse.getAllHeaders();  
  111.                 for(Header h : headers){  
  112.                     System.err.println(h.getName() + " : " + h.getValue());  
  113.                 }  
  114.                 HttpEntity entity = httpResponse.getEntity();  
  115.                 return EntityUtils.toString(entity);  
  116.             }  
  117.             return null;  
  118.         } catch (Exception e) {  
  119.             e.printStackTrace();  
  120.             return null;  
  121.         } finally {  
  122.             try {  
  123.                 if (ois != null) {  
  124.                     ois.close();  
  125.                 }  
  126.             } catch (IOException e) {  
  127.                 e.printStackTrace();  
  128.             }  
  129.             httpClient.getConnectionManager().shutdown();  
  130.         }  
  131.     }  
  132.   
  133.     /**  
  134.      * Set the connection timeout for the underlying HttpClient. A timeout value  
  135.      * of 0 specifies an infinite timeout.  
  136.      *   
  137.      * @param timeout the timeout value in milliseconds  
  138.      */    
  139.     private static void setConnectTimeout(int timeout) {    
  140.         httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,    
  141.                 timeout);    
  142.     }    
  143.     
  144.     /**  
  145.      * Set the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout  
  146.      * for waiting for data or, put differently, a maximum period inactivity  
  147.      * between two consecutive data packets.A timeout value of 0 specifies an  
  148.      * infinite timeout.  
  149.      *   
  150.      * @param timeout the timeout value in milliseconds  
  151.      */    
  152.     private static void setReadTimeout(int timeout) {    
  153.         httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);    
  154.     }  
  155.       
  156.   
  157.     private static void releaseConnection() {  
  158.         if (httpClient != null)  
  159.             httpClient.getConnectionManager().shutdown();  
  160.     }  
  161.       
  162.   

0 0
原创粉丝点击