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 ...
随机推荐
- Don't Repeat Yourself (不要重复你自己)
DRY是指Don't Repeat Yourself特指在程序设计以及计算中避免重复代码,因为这样会降低灵活性.简洁性,并且可能导致代码之间的矛盾.<The Pragmatic Programm ...
- BZOJ 1079: [SCOI2008]着色方案 记忆化搜索
1079: [SCOI2008]着色方案 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...
- CDOJ 481 Apparent Magnitude 水题
Apparent Magnitude Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/sh ...
- 使用CKEditor编辑器进行文本编辑
首先下载CKEditor. 下载地址:点击打开链接 将下载完的CKEditor解压而且导入到项目中. 然后在页面引入CKEditor <script type="text/javasc ...
- 有图有真相,分享一款网页版HTML5飞机射击游戏
本飞机射击游戏是使用HTML5代码写的,尝试通过统一开发环境(UDE)将游戏托管在MM应用引擎,直接生成了网页版游戏,游戏简单易上手,非常适合用来当做小休闲打发时间. 游戏地址:http://flyg ...
- [Javascript] Maybe Functor
In normal Javascript, we do undefine check or null check: , name: "Suvi"}; var name = pers ...
- hibernate 实体关系映射笔记
@经常使用属性说明: @Entity:实体类 @Table:指定相应数据表 @Id:主键,使用能够为null值的类型,假设实体类没有保存到数据库是一个暂时状态 @Col ...
- boost.asio源码剖析(一) ---- 前 言
* 前言 源码之前,了无秘密. ——侯捷 Boost库是一个可移植.提供源代码的C++库,作 ...
- mybatis0212 mybatis逆向工程 (MyBatis Generator)
1mybatis逆向工程 (MyBatis Generator) .1什么是mybatis的逆向工程 mybatis官方为了提高开发效率,提高自动对单表生成sql,包括生成 :mapper.xml.m ...
- MySQL并发复制系列三:MySQL和MariaDB实现对比
http://blog.itpub.net/28218939/viewspace-1975856/ 并发复制(Parallel Replication) 系列三:MySQL 5.7 和MariaDB ...