java查询WFS服务
在我们访问wfs服务时候,有时候会遇到前台访问时候的跨域问题。这里给出java访问的一个小例子。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.QName; public class WFSUtil
{ /**
* 得到过滤结果。名字默认是Poi
*
* @param keyWords
* @param wfsUrl
* @return
* @throws HttpException
* @throws IOException
*/
public String findPointsXmlByKeywords(String[] keyWords, String wfsUrl,
String wfsTitle, String layerName) throws HttpException,
IOException
{
if (keyWords.length==0)
{
return "<root/>";
} if (wfsTitle == null)
{
wfsTitle = "ST";
}
if (layerName == null)
{
layerName = "POI";
}
StringBuffer buffer = new StringBuffer(
"<?xml version='1.0' encoding='UTF-8'?>"
+ "<wfs:GetFeature maxFeatures='100' service='WFS' version='1.1.0' "
+ "xmlns:wfs='http://www.opengis.net/wfs' "
+ "xmlns:gml='http://www.opengis.net/gml' "
+ "xmlns:ogc='http://www.opengis.net/ogc' "
+ "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
+ "xsi:schemaLocation='http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd'>"
+ "<wfs:Query typeName='" + layerName
+ "' srsName='EPSG:4326'>"
+ "<ogc:Filter xmlns:ogc='http://www.opengis.net/ogc'>"); if (keyWords.length > 1)
{
buffer.append("<ogc:Or>");
for (int i = 0; i < keyWords.length; i++)
{
buffer.append(addPropertyIsLike(keyWords[i]));
}
buffer.append("</ogc:Or>");
}
else if (keyWords.length == 1)
{
buffer.append(addPropertyIsLike(keyWords[0]));
}
else
{
return null;
} buffer.append("</ogc:Filter></wfs:Query></wfs:GetFeature>");
PostMethod post = new PostMethod(wfsUrl);
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
"utf-8");
post.setRequestEntity(new StringRequestEntity(buffer.toString(),
"application/xml", "UTF-8"));
HttpClient httpclient = new HttpClient(); httpclient.executeMethod(post); InputStream xmlis = post.getResponseBodyAsStream();
//确定读取编码为utf-8,不设置的话会用系统默认编码,例如在tomcat容器中,就可能乱码。
BufferedReader br = new BufferedReader(new InputStreamReader(xmlis,"utf-8")); String tempbf;
StringBuffer html = new StringBuffer(100);
while ((tempbf = br.readLine()) != null)
{
html.append(tempbf);
}
// System.out.println(html.toString());
return formatPoint(html.toString(),keyWords[0]);
} private String addPropertyIsLike(String word)
{
String result = "";
if (word != null && (!word.trim().isEmpty()))
{
result += "<ogc:PropertyIsLike wildCard='*' singleChar='.' escape='!'><ogc:PropertyName>POI_NAME</ogc:PropertyName><ogc:Literal>*"
+ word + "*</ogc:Literal></ogc:PropertyIsLike>";
}
return result;
} private String formatPoint(String xml,String keyWord)
{
try
{
Document doc = DocumentHelper.parseText(xml); Element root = doc.getRootElement(); Element newRoot = DocumentHelper.createElement("root");
newRoot.addAttribute("keyword", keyWord); List<Element> list = root.elements("featureMember"); for (Element e : list)
{
Element point = newRoot.addElement("point");
Element xs = e.element("Poi").element("X");
Element ys = e.element("Poi").element("Y");
if (xs != null&&ys!=null)
{
String text=xs.getText()+","+ys.getText();
point.addElement("xys").setText(text);
point.addElement("type").setText("point");
//point.addElement("x").setText(xs.getText());
//point.addElement("y").setText(ys.getText());
}
point.addElement("name").setText(
e.element("Poi").element("POI_NAME").getText());
point.addElement("address").setText(
e.element("Poi").element("ADDRESS").getText());
}
return newRoot.asXML();
// throw new Exception("formatPoint is fail");
}
catch (DocumentException e)
{
// TODO Auto-generated catch block
System.out.println("格式化失败!");
e.printStackTrace();
} return null;
} }
这里面用到了commons.HttpClientj-3.1.jar和dom4j这两个jar包,如果是不同版本的jar包,写法可能略有不同。
欢迎交流:http://www.cnblogs.com/shizhongtao/p/3468664.html
有时间再整理下……
java查询WFS服务的更多相关文章
- WFS服务查询方法
基于Geoserver发布的wfs服务,实现空间和属性信息的查询.wfs包含getFeature操作,用来检索要素信息,支持返回gml格式的地理要素表达. WFS的getFeature操作需要提供的参 ...
- cesium结合geoserver利用WFS服务实现图层新增(附源码下载)
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...
- leaflet结合geoserver利用WFS服务实现图层删除功能(附源码下载)
前言 leaflet 入门开发系列环境知识点了解: leaflet api文档介绍,详细介绍 leaflet 每个类的函数以及属性等等 leaflet 在线例子 leaflet 插件,leaflet ...
- openlayers6结合geoserver利用WFS服务实现图层新增功能(附源码下载)
内容概览 1.openlayers6结合geoserver利用WFS服务实现图层新增功能2.源代码demo下载 效果图如下: 本篇主要是openlayers6通过调用geoserver发布的地图服务W ...
- leaflet结合geoserver利用WFS服务实现图层新增功能(附源码下载)
前言 leaflet 入门开发系列环境知识点了解: leaflet api文档介绍,详细介绍 leaflet 每个类的函数以及属性等等 leaflet 在线例子 leaflet 插件,leaflet ...
- cesium结合geoserver利用WFS服务实现图层编辑(附源码下载)
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...
- cesium结合geoserver利用WFS服务实现图层删除(附源码下载)
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...
- Java查询大文本
但JAVA本身缺少相应的类库,需要硬编码才能实现结构化文件计算,代码复杂且可读性差,难以实现高效的并行处理. 使用免费的集算器可以弥补这一不足.集算器封装了丰富的结构化文件读写和游标计算函数,书写简单 ...
- OpenLayers调用ArcGIS Server发布的WFS服务
OpenLayers调用ArcGIS Server发布的WFS服务 原创: 蔡建良 2013-08-20 一. 开发环境 1) Openlayers2.13+arcgis server9.3 2) W ...
随机推荐
- CreateProcess的使用方法
使用编译器vs2008. 第一.第二个參数的使用方法: 样例: 使用ie打开指定的网页. 注意第二个參数是 可运行文件+命令行參数 #include "stdafx.h" #inc ...
- 首发Zend Studio 10.6正式版注册破解(2014-02-06更新)
1.写在前头以下文章没有图片说明,如果你没有耐性看或想看图文并茂的图片文章请绕道,谢谢配合.转发请标明转自http://www.geekso.com/ZendStudio100/以下方法仅供技术交流学 ...
- Maven 插件开发(一)
项目在重构之后,想由ant切换到maven,在转换的过程中遇到一个问题.因为项目是基于OSGi的架构,而OSGi在运行时是依赖于插件环境的,bundle之间存在package依赖.而maven是基于d ...
- 解决Server Error in '/' Application.方法!
<!-- Web.Config Configuration File --> <configuration> <system.web> ...
- Gradient Boost Decision Tree(&Treelink)
http://www.cnblogs.com/joneswood/archive/2012/03/04/2379615.html 1. 什么是Treelink Treelink是阿里集团内部 ...
- 实例源码--Android的ListView控件的总结
下载源码 技术要点: 1.ListView控件的总结 2.微信ListView气泡的实现 3.ListView仿优酷播放列表 4.ListView刷新列表 5.详细的源码注释 ...... 详细介 ...
- c语言海量数据处理
教你如何迅速秒杀掉:99%的海量数据处理面试题 http://wenku.baidu.com/view/4546d06ca45177232f60a276.html c语言如何对海量数据进行处理 PDF ...
- 分布式ActiveMQ集群--转载
原文地址:http://shensy.iteye.com/blog/1752529 回顾总结前一段时间学习的ActiveMQ分布式集群相关的知识,分享出来希望对看到的人有所帮助. 一.分布式Activ ...
- 12096 - The SetStack Computer UVA
Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German ...
- [转]DataTable用中使用Compute 实现简单的DataTable数据的统计
本文转自:http://blog.csdn.net/zwxrain/article/details/252285 調用格式: object DataTable.Compute(string expre ...