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

   在geoserver自定义的地图中通过geoserver wfs 查询,删除,添加相关的POI。

相关操作的格式如下:

查询

       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">   
          
         
      
      status
      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">   
      
         
            
               
                             13404701.212,3850391.781
               

            

             13
             12
             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">   
      
       
       qqybh
       12
     

         
      
      qqybh
      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">   
      
         
      
      qqybh
      12
      

      
      qqybh
      0
      

     
   
      
   
    
;

 

 

具体实现如下:

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 params=new HashMap();
		 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("   "); 
		sb.append(" ");
		sb.append("  ");
		sb.append("    ");
		sb.append("     ");
		sb.append(" 	"+lat+","+lon+"");
		sb.append("   ");
		sb.append(" ");
		
		if(!params.isEmpty()){
			for (Entry entry : params.entrySet()) {
				sb.append("      "+entry.getValue()+"");
			}
		}
		sb.append("  ");
		sb.append("    ");
		sb.append(" ");
		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 params=new HashMap();
		 params.put("Name", "cesuo");
		 
		StringBuffer sb = new StringBuffer();
		sb.append("");
		sb.append(" ");
		sb.append("  ");
		sb.append("     ");
		if(!params.isEmpty()){
			for (Entry entry : params.entrySet()) {
				sb.append("      f:"+entry.getKey()+"");
				sb.append("       "+entry.getValue()+"");
			}
		}
		sb.append("   ");
		sb.append(" ");
		sb.append(" ");
		sb.append("");
		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 params=new HashMap();
		 params.put("Name", "cesuo");
		
		StringBuffer sb = new StringBuffer();
		sb.append("");
		sb.append("   ");
		sb.append("  ");
		sb.append("    ");
		if(!params.isEmpty()){
			for (Entry entry : params.entrySet()) {
				sb.append("      f:"+entry.getKey()+"");
				sb.append("       "+entry.getValue()+"");
			}
		}
		sb.append("   ");
		sb.append("        ");
		sb.append("     ");
		sb.append(" ");
		String response = HttpUtils.doGeoServerPOST(
				"http://10.100.108.20:8080/geoserver/wfs?outputFormat=json",
				sb.toString());
		System.out.println("response:");
		System.out.println("" + response);
	}
}

 

http工具类:
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();
    }
    

}
 

你可能感兴趣的:(GIS&Mobile&MAP,geoserver,wfs:Query,wfs:Delete,wfs:Update,wfs:Insert)