📄 wfsggazetteerservice.java
字号:
package com.esri.solutions.jitk.common.gazetteer;
import com.esri.adf.web.data.geometry.WebGeometry;
import com.esri.adf.web.data.geometry.WebPoint;
import com.esri.adf.web.data.geometry.WebPolygon;
import com.esri.adf.web.data.geometry.WebRing;
import com.esri.adf.web.data.geometry.WebSpatialReference;
import org.apache.log4j.Logger;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
* Concrete implementation of the {@link IGazetteerService} interface. This
* class represents a WFS-G OGC compliant Gazetteer service.
*/
public class WfsgGazetteerService implements IGazetteerService {
/**
* {@link Logger} used to log messages for this class.
*/
private static final Logger _logger = Logger.getLogger(WfsgGazetteerService.class);
/**
* Name of the Gazetteer service.
*/
private String m_name = null;
/**
* Connecting URL of the Gazetteer service.
*/
private String m_serviceUrl = null;
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.gazetteer.IGazetteerService#getName()
*/
public String getName() {
return m_name;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.gazetteer.IGazetteerService#setName(java.lang.String)
*/
public void setName(String name) {
m_name = name;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.gazetteer.IGazetteerService#getServiceUrl()
*/
public String getServiceUrl() {
return m_serviceUrl;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.gazetteer.IGazetteerService#setServiceUrl(java.lang.String)
*/
public void setServiceUrl(String url) {
m_serviceUrl = url;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.gazetteer.IGazetteerService#search(java.lang.String, int)
*/
public ArrayList<GazetteerSearchResult> search(String placeName,
int numResults, SearchMethod searchMethod) {
ArrayList<GazetteerSearchResult> results = null;
if (placeName == null) {
throw new NullPointerException();
}
try {
results = sendWfsgRequest(placeName, numResults, searchMethod);
} catch (Exception ex) {
_logger.warn("Exception occurred sending WFS request.", ex);
}
return results;
}
/**
* Sends a get feature request to the WFS-G service using the
* {@link WFSGClient}. The actual request is create via {@link WFSGClient#createGetFeaturesRequest(String, int)}
* and the request is sent using {@link WFSGClient#submitGetFeatureRequest(String)}.
*
* @param placeName Place name search value.
* @param numResults Number of max results requested.
* @return {@link ArrayList} of {@link GazetteerSearchResult} that represents the records returned
* from the get feature request.
*/
private ArrayList<GazetteerSearchResult> sendWfsgRequest(String placeName,
int numResults, SearchMethod searchMethod) {
ArrayList<GazetteerSearchResult> results = null;
WFSGClient client = null;
if (placeName == null) {
throw new NullPointerException();
}
client = new WFSGClient(getServiceUrl());
// create the GetFeature request
client.createGetFeaturesRequest(placeName, numResults, searchMethod);
_logger.debug("WFS-G request: " + client.getGetFeaturesRequest());
// submit the request
client.submitGetFeatureRequest(client.getGetFeaturesRequest());
_logger.debug("WFS-G response: " + client.getGetFeaturesResponse());
results = parseWfsgResponse(client.getGetFeaturesResponse());
return results;
}
/**
* Using the Document Object Model (DOM), parses the WFS-G get records response
* into an {@link ArrayList} of {@link GazetteerSearchResult}.
*
* @param response The XML WFS-G get records response returned from the WFS-G service.
* @return {@link ArrayList} of {@link GazetteerSearchResult} that represents the results
* returned from the WFS-G get records response.
*/
private ArrayList<GazetteerSearchResult> parseWfsgResponse(String response) {
ArrayList<GazetteerSearchResult> results = null;
String srsUrn = null;
String srsId = null;
int sprId;
WebSpatialReference adfSpr = null;
SAXParserFactory factory = null;
SAXParser saxParser = null;
InputSource is = null;
WfsgGetFeatureResponseHandler handler = null;
results = new ArrayList<GazetteerSearchResult>();
// create the WFS-G GetFeature response default handler
handler = new WfsgGetFeatureResponseHandler();
// create SAXParserFactory and set properties
factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
try {
// parse the GetFeature response
saxParser = factory.newSAXParser();
is = new InputSource(new ByteArrayInputStream(response.getBytes()));
saxParser.parse(is, handler);
srsUrn = handler.getSrsId();
srsId = srsUrn.substring(srsUrn.lastIndexOf(":") + 1);
try {
sprId = Integer.valueOf(srsId);
adfSpr = WebSpatialReference.getWebSpatialReference(sprId);
} catch (NumberFormatException ex) {
_logger.warn("Invalid GML spatial reference - defaulting to EPSG 4326",
ex);
adfSpr = WebSpatialReference.getWebSpatialReference(4326);
}
String thePlace = null;
WebGeometry geom = null;
GazetteerSearchResult gsr = null;
Map<String, String> details = null;
for (GazetteerPlace gp : handler.getPlaces()) {
thePlace = gp.getPlaceName();
geom = this.getExtentFromPosList(gp.getPlaceLocation());
// set the spatial reference on the geometry
geom.setSpatialReference(adfSpr);
gsr = new GazetteerSearchResult(geom);
gsr.setName(thePlace);
details = new LinkedHashMap<String, String>();
details.put("Name", thePlace);
gsr.setDetails(details);
results.add(gsr);
}
} catch (IOException ex) {
_logger.warn("IOException occurred parsing WFS response.", ex);
_logger.debug("IOException occurred parsing WFS response: [" +
response + "]", ex);
} catch (ParserConfigurationException ex) {
_logger.warn("ParserConfigurationException occurred parsing WFS response.",
ex);
_logger.debug(
"ParserConfigurationException occurred parsing WFS response: [" +
response + "]", ex);
} catch (SAXException ex) {
_logger.warn("SAXException occurred parsing WFS response.", ex);
_logger.debug("SAXException occurred parsing WFS response: [" +
response + "]", ex);
}
return results;
}
/**
* Utility method to parse the geography location of a WFS-G result
* record from the given <code>posList</code>.
*
* @param posList {@link String} comma delimited list of geographic points.
* @return Depending on the location type the {@link WebGeometry} returned could be of type
* {@link WebPoint} or {@link WebPolygon}.
*/
private WebGeometry getExtentFromPosList(String posList) {
WebGeometry geom = null;
String strInput = posList;
strInput = strInput.replace(",", " ");
strInput = strInput.trim();
String[] strCoordinateList = new String[countSpaces(strInput)];
strCoordinateList = strInput.split(" ");
if (strCoordinateList.length == 2) {
WebPoint point = new WebPoint();
point.setY(Double.valueOf(strCoordinateList[0]));
point.setX(Double.valueOf(strCoordinateList[1]));
geom = point;
} else {
WebPolygon polygon = new WebPolygon();
WebRing ring = new WebRing();
for (int i = 0; i < (strCoordinateList.length - 1); i = i + 2) {
WebPoint point = new WebPoint();
point.setY(Double.valueOf(strCoordinateList[i]));
point.setX(Double.valueOf(strCoordinateList[i + 1]));
ring.addPoint(point);
}
polygon.addRing(ring);
geom = polygon;
}
return geom;
}
/**
* Utility method to count the number of spaces in the given
* <code>strInput</code>.
*
* @param strInput {@link String} in question.
* @return The number of spaces in the <code>strInput</code>.
*/
private int countSpaces(String strInput) {
int withSpaces = strInput.length();
int withoutSpaces = strInput.replace(" ", "").length();
return withSpaces - withoutSpaces;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -