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 ...
随机推荐
- -_-#【JS】defer / async
引用JavaScript文件时的两个属性defer和async <script src="js1.js" defer></script> <scrip ...
- MySQL · 物理备份 · Percona XtraBackup 备份原理
http://mysql.taobao.org/monthly/2016/03/07/ 前言 Percona XtraBackup(简称PXB)是 Percona 公司开发的一个用于 MySQL 数据 ...
- 苹果开发工具:Xcode和Interface Builder
摘要:在Mac OS X上开发,首选Xcode继承开发环境.本文对开发工具套件中最重要的Xcode和Interface Builder两个工具做了简要介绍,并提供<苹果开发工具概述>PDF ...
- 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "StdAfx.h"”?
在查找预编译头时遇到意外的文件结尾.是否忘记了向源中添加“#include "StdAfx.h"”? 右键选择该文件.cpp格式的->属性->预编译头,→ 不使用预编译 ...
- Windows 7中使用Eclipse 使用CDT and WinGW 开发C/C++(转载)
以前使用visual studio 2010编写C/C++,后来接触了Eclipse后,据说eclipse也可以编写C/C++,以前觉得Visual studio 2010还蛮不错的,也没有多大好奇心 ...
- 解决ajax回调函数无返回值得问题
这里以编辑验证角色名为例: 首先,定义一个flag全局变量. 然后,在回调函数resp()中根据判断将flag的值设为true或false. 最后,在调用函数ckrname()中,return fla ...
- tomcat服务器 去掉端口8080 以及项目名 直接使用IP地址访问
>>> 先在web.xml中设置默认页面 <welcome-file-list> <welcome-file>/login.jsp</welcom ...
- App Thinning
App Thinning 由于项目中需要开启Bitcode编译,之前对Bitcode也有些误区,故整理了下相关知识,仅供参考,如有不对,还请指出. 当前 iOS App 的编译打包方式是把适配兼容多个 ...
- ganglia监控hadoop2.0配置方法
ganglia监控hadoop2.0配置方法前提:hadoop2.0集群已安装ganglia监控工具第一步:Hadoop用户登录集群每一个节点,修改文件:vi /opt/hadoop-2.0.0-cd ...
- 去掉VC2010 编辑器里出现的红色波浪线
在VC2010中浏览代码的时候就大片的红线看着不舒服 其实不关VS的事,原因在于visual assist. 在VAssistX菜单栏->Visual Assist X Options-&g ...