您的位置:首页 > 编程语言 > Java开发

Java读取GIS服务器数据

2013-09-15 16:40 316 查看
package com.gis.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.map.ObjectMapper;
import com.gis.entity.Attributes;
import com.gis.entity.Geometry;
import com.gis.entity.HeavyMetal;
import com.gis.rest.RestAPI;
import com.gis.util.Constants;
public class QueryAction {
public HeavyMetal queryByObjectid(int objectid) {
HeavyMetal heavyMetal = new HeavyMetal();
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("f", "json"));
//queryurl=http://192.168.24.218/ArcGIS/rest/services/HeavyMetal/FeatureServer/0/11(11是objectid)
String response = RestAPI.rest(Constants.queryurl + objectid,
formparams);
JSONObject jsonObject = JSONObject.fromObject(response);
Map map = new HashMap();
for (Iterator iter = jsonObject.keys(); iter.hasNext();) {
String key = (String) iter.next();
map.put(key, jsonObject.get(key));
}
System.out.println("feature:" + map.get("feature"));
ObjectMapper objectMapper = new ObjectMapper();
String str = map.get("feature").toString();
JSONObject jsonObject1 = JSONObject.fromObject(str);
Map map1 = new HashMap();
String[] strings = new String[2];
int i = 0;
for (Iterator iter = jsonObject1.keys(); iter.hasNext();) {
// System.out.println(iter);
String key = (String) iter.next();
map.put(key, jsonObject1.get(key));
// System.err.println(jsonObject1.get(key));
strings[i] = jsonObject1.get(key).toString();
i++;
}
JSONObject obj = new JSONObject().fromObject(strings[0]);
// System.err.println("OBJECTID:" + obj.get("OBJECTID"));
Attributes attributes = (Attributes) JSONObject.toBean(obj,
Attributes.class);
obj = new JSONObject().fromObject(strings[1]);
Geometry geometry = (Geometry) JSONObject.toBean(obj, Geometry.class);
heavyMetal.setAttributes(attributes);
heavyMetal.setGeometry(geometry);
return heavyMetal;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
QueryAction queryAction = new QueryAction();
System.err.println(queryAction.queryByObjectid(11));
}
}

public class RestAPI {
public static String rest(String url,List<NameValuePair> formparams)
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);

String responseBody = null;
BufferedReader br = null;
try {
UrlEncodedFormEntity uef = new UrlEncodedFormEntity(formparams, HTTP.UTF_8);
post.setEntity(uef);
ResponseHandler<String> responseHandler = new BasicResponseHandler();

responseBody = httpClient.execute(post, responseHandler);
System.out.println(responseBody);
}
catch (Exception e) {
// TODO: handle exception
}
return responseBody;
}
}


response值

{"feature":{"attributes":{"OBJECTID":11,"ID":1,"CD":0.2433,"HG":0.2433,"DP_AS":1.0309,"CU":2.3848,"PB":217.214,"CR":353.527,"ZN":357.4679,"NI":208.2775,"HM_TIME":1379085429000,"HM_NAME":"宜昌","USER_NAME" : null},"geometry":{"x":111.300000000745,"y":31.1999999992549}}}
解析一次之后的str值

feature:{"attributes":{"OBJECTID":11,"ID":1,"CD":0.2433,"HG":0.2433,"DP_AS":1.0309,"CU":2.3848,"PB":217.214,"CR":353.527,"ZN":357.4679,"NI":208.2775,"HM_TIME":1379085429000,"HM_NAME":"宜昌","USER_NAME":null},"geometry":{"x":111.3,"y":31.2}}
再分别解析出attributes和geometry,最终得到解析出的对象

demo地址http://down.51cto.com/data/947003
本文出自 “优赛工作室” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1297385
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: