大体可以分为5个步骤:
1、建立http客户端
2、设置请求参数
3、画出圆图形,并转换为json
4、相交查询操作
5、解析返回json
public void xiangjiao() throws MismatchedDimensionException, FactoryException, TransformException{
String url="";请求的图层url
//创建一个http客户端
HttpClient client=new DefaultHttpClient();//创建一个POST请求
HttpPost request=new HttpPost(url);
//设置HTTP POST请求参数必须用NameValuePair
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("f", "json"));//format设置成json
//查询类型-相交查询
params.add(new BasicNameValuePair("spatialRel","esriSpatialRelIntersects"));
//相交查询
params.add(new BasicNameValuePair("geometryType","esriGeometryPolygon"));
//圆的中心点坐标
double X=120.01075948180949;//经度
double Y=33.37515510419829;//纬度
//EPSG:4326,等同于WGS84坐标系(球面坐标)
//EPSG:3857,等同于900913,由Mercator投影而来,经常用于web地图(平面坐标)
Point p=transform(X,Y,"EPSG:4326","EPSG:3857");//转换为平面坐标系
//以点和半径生成wkt圆Double radius=100.0;//半径假设为100.0米
int pointNum=30;
// 生成wkt字符串
String wktStr="POLYGON ((";int startAngle=0;
int endAngle=360;
for ( int j = 0; j <= pointNum; j++) {
int angle = startAngle + (endAngle - startAngle) * j
/ pointNum;
Double sin = Math.sin(angle * Math.PI / 180);
Double cos = Math.cos(angle * Math.PI / 180);
String x = Double.toString(p.getX() + radius * sin);
String y = Double.toString(p.getY()+ radius * cos);
if(j==0){
wktStr+=x+" "+y;
}else{
wktStr+=","+x+" "+y;
}
}
wktStr+="))";
System.out.print(wktStr);
//将wkt转换为json后进行查询
String resultjson=polygonwkttoJson(wktStr);
params.add(new BasicNameValuePair("geometry",resultjson));//查询图形
params.add(new BasicNameValuePair("outFields","*"));//查询所有字段
try {
//设置http Post请求参数
HttpEntity entity = new UrlEncodedFormEntity(params);
request.setEntity(entity);
HttpResponse response=client.execute(request);
if(response.getStatusLine().getStatusCode()==200){//如果状态码为200,就是正常返回
String result=EntityUtils.toString(response.getEntity());
System.out.println(result);//返回的是一个json字符串
}
} catch (ClientProtocolException e) {
e.printStackTrace();
//进行处理操作
} catch (IOException e) {
//进行处理操作
}
}
//将wkt转换为json
private static String polygonwkttoJson(String wkt) {
String jsonT = "{\"geometry\":{\"wkid\":3857},\"rings\":[[%s]]}";
String regex = ".+?\\(\\((.+?)\\)\\)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(wkt);
if (matcher.matches()) {
String group = matcher.group(1).trim();
String[] xys = group.split(",");
String[] partArray = new String[xys.length];
for(int i = 0; i < xys.length; i++){
String[] xy = xys[i].trim().split(" ");
partArray[i] = String.format("[%s,%s]", xy[0], xy[1]);
}
String coords = join(partArray, ',');
return String.format(jsonT, coords);
} else {
return "";
}
}
public static String join(Object[] array, char separator) {
if (array == null) {
return null;
}
int arraySize = array.length;
int bufSize = (arraySize == 0 ? 0 : ((array[0] == null ? 16 : array[0].toString().length()) + 1) * arraySize);
StringBuffer buf = new StringBuffer(bufSize);
for (int i = 0; i < arraySize; i++) {
if (i > 0) {
buf.append(separator);
}
if (array[i] != null) {
buf.append(array[i]);
}
}
return buf.toString();
}
//坐标转换
public static Point transform(double lon, double lat,
String epsgSource, String epsgTarget) throws FactoryException,
MismatchedDimensionException, TransformException {
Coordinate sourceCoord = new Coordinate(lon, lat);
GeometryFactory geoFactory = new GeometryFactory();
Point sourcePoint = geoFactory.createPoint(sourceCoord);
CoordinateReferenceSystem crsSource = CRS.decode(epsgSource);
CoordinateReferenceSystem crsTarget = CRS.decode(epsgTarget);
MathTransform transform = CRS.findMathTransform(crsSource, crsTarget);
Point pointTarget = (Point) JTS.transform(sourcePoint, transform);
//System.out.println(pointTarget.getX()+" "+pointTarget.getY());
return pointTarget;
}