zoukankan      html  css  js  c++  java
  • 关于JAVA通过REST接口对arcGis Server数据进行增删改查

    一: 添加要素

     1 public void create(BoxVo boxVo) throws Exception {
     2     // 创建HTTP客户端
     3     CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(ArcGisConstants.REQUEST_CONFIG).build();
     4     // 创建POST请求
     5     HttpPost httpPost = new HttpPost(ArcGisConstants.baseDataUrl + ArcGisConstants.BOX_ADD_FEATURES);
     6     // 查询当前新增的box在ArcGis中是否存在
     7     Map<String, Object> oldFeature = query(boxVo);
     8     if (oldFeature != null && oldFeature.size() > 0) {
     9       LOGGER.info("当前新增的box已经存在, 执行修改操作");
    10       update(boxVo);
    11       return;
    12     }
    13     // 运行到这里说明该box在ArcGis中不存在, 执行新增操作
    14     // 创建新的空间模型
    15     Map<String, Object> geometry = ArcGisUtils.createNewGeometry(boxVo.getLongitude(), boxVo.getLatitude());
    16     // 创建新的数据模型
    17     Map<String, Object> attributes = ArcGisUtils.createNewBoxAttributes(boxVo, null);
    18     // 创建新的容器模型
    19     List<Map<String, Object>> features = ArcGisUtils.createNewFeatures(geometry, attributes);
    20     // 模型对象转换成json字符串
    21     String jsonStr = JsonUtil.jsonObj2Sting(features);
    22     // 创建POST请求参数, 必须用NameValuePair
    23     List<NameValuePair> params = new ArrayList<>();
    24     // 设置传值参数类型为json
    25     params.add(new BasicNameValuePair("f", "json"));
    26     // 将转换好的字符串添加到参数中
    27     params.add(new BasicNameValuePair(ArcGisConstants.FEATURES, jsonStr));
    28     // 设置POST请求参数,并将参数格式设置为utf-8
    29     HttpEntity entity = new UrlEncodedFormEntity(params, ArcGisConstants.DEFAULT_CHARSET);
    30     httpPost.setEntity(entity);
    31     // 发送请求
    32     HttpResponse response = httpclient.execute(httpPost);
    33     // 处理结果集
    34     if (response.getStatusLine().getStatusCode() == 200) {
    35       // 调用EntityUtils.toString方法最后会自动把inputStream close掉的
    36       String str = EntityUtils.toString(response.getEntity());
    37       LOGGER.info("ArcGis添加box: box: " + jsonStr + " 结果: " + str);
    38       // 释放资源
    39       httpclient.close();
    40     } else {
    41       throw new RuntimeException("ArcGis添加box失败");
    42     }
    43   }

    二: 更改要素

     1 public void update(BoxVo boxVo) throws Exception {
     2     // 创建HTTP客户端
     3     CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(ArcGisConstants.REQUEST_CONFIG).build();
     4     // 创建post请求
     5     HttpPost httpPost = new HttpPost(ArcGisConstants.baseDataUrl + ArcGisConstants.BOX_UPDATE_FEATURES);
     6     // 查询当前被修改的box在ArcGis中是否存在
     7     Map<String, Object> oldFeature = query(boxVo);
     8     if (oldFeature != null && oldFeature.size() > 0) {
     9       // 得到feature里面的attributes
    10       Map<String, Object> oldAttributes = (Map<String, Object>) oldFeature.get(ArcGisConstants.ATTRIBUTES);
    11       // 得到attributes里面的objectid
    12       Integer objectid = (Integer) oldAttributes.get(ArcGisConstants.OBJECT_ID);
    13       if (objectid != null && objectid != 0) {
    14         // 创建新的空间模型
    15         Map<String, Object> newGeometry = ArcGisUtils.createNewGeometry(boxVo.getLongitude(), boxVo.getLatitude());
    16         // 创建新的数据模型
    17         Map<String, Object> newAttributes = ArcGisUtils.createNewBoxAttributes(boxVo, objectid);
    18         // 创建新的容器模型
    19         List<Map<String, Object>> newFeatures = ArcGisUtils.createNewFeatures(newGeometry, newAttributes);
    20         // 将模型对象转换成json字符串
    21         String jsonStr = JsonUtil.jsonObj2Sting(newFeatures);
    22         // 创建POST请求参数, 必须用NameValuePair
    23         List<NameValuePair> params = new ArrayList<>();
    24         // 设置传值参数类型为json
    25         params.add(new BasicNameValuePair("f", "json"));
    26         // 将转换好的字符串添加到参数中
    27         params.add(new BasicNameValuePair(ArcGisConstants.FEATURES, jsonStr));
    28         // 设置POST请求参数,并将参数格式设置为utf-8
    29         HttpEntity entity = new UrlEncodedFormEntity(params, ArcGisConstants.DEFAULT_CHARSET);
    30         httpPost.setEntity(entity);
    31         // 发送请求
    32         HttpResponse response = httpclient.execute(httpPost);
    33         // 处理结果集
    34         if (response.getStatusLine().getStatusCode() == 200) {
    35           // 调用EntityUtils.toString方法最后会自动把inputStream close掉的
    36           EntityUtils.toString(response.getEntity());
    37           LOGGER.info("ArcGis更改box: mapid = " + boxVo.getMapId());
    38           // 释放资源
    39           httpclient.close();
    40         } else {
    41           throw new RuntimeException("ArcGis更改box失败");
    42         }
    43       } else {
    44         create(boxVo);
    45       }
    46     } else {
    47       // 如果查出来的结果为null,则将该box添加到arcGis中图层
    48       create(boxVo);
    49     }
    50   }

    三: 删除要素

     1   public void delete(List<BoxVo> boxVos) throws Exception {
     2     // 创建HTTP客户端
     3     CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(ArcGisConstants.REQUEST_CONFIG).build();
     4     // 创建POST请求
     5     HttpPost httpPost = new HttpPost(ArcGisConstants.baseDataUrl + ArcGisConstants.BOX_DELETE_FEATURES);
     6     // 处理请求参数
     7     StringBuffer deleteWhere = new StringBuffer();
     8     // 拼装条件, 按照mapIds进行批量删除
     9     deleteWhere.append("mapid in (");
    10     if (boxVos != null && boxVos.size() > 0) {
    11       for (int i = 0; i <= boxVos.size() - 1; i++) {
    12         deleteWhere.append("'");
    13         deleteWhere.append(boxVos.get(i).getMapId());
    14         deleteWhere.append("'");
    15         if (i != boxVos.size() - 1) {
    16           deleteWhere.append(",");
    17         }
    18       }
    19     }
    20     deleteWhere.append(")");
    21     // 创建POST请求参数, 必须用NameValuePair
    22     List<NameValuePair> params = new ArrayList<>();
    23     // 设置传值参数类型为json
    24     params.add(new BasicNameValuePair("f", "json"));
    25     // 将拼装好的条件添加到参数中
    26     params.add(new BasicNameValuePair("where", deleteWhere.toString()));
    27     // 设置POST请求参数,并将参数格式设置为utf-8
    28     HttpEntity entity = new UrlEncodedFormEntity(params, ArcGisConstants.DEFAULT_CHARSET);
    29     httpPost.setEntity(entity);
    30     // 发送请求
    31     HttpResponse response = httpclient.execute(httpPost);
    32     if (response.getStatusLine().getStatusCode() == 200) {
    33       // 调用EntityUtils.toString方法最后会自动把inputStream close掉的
    34       EntityUtils.toString(response.getEntity());
    35       LOGGER.info("ArcGis批量删除box: " + deleteWhere.toString());
    36       // 释放资源
    37       httpclient.close();
    38     } else {
    39       throw new RuntimeException("ArcGis删除box失败");
    40     }
    41   }

     四: 查询要素

     1   public Map<String, Object> query(BoxVo boxVo) throws Exception {
     2     // 创建HTTP客户端
     3     CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(ArcGisConstants.REQUEST_CONFIG).build();
     4     // 创建POST请求
     5     HttpPost httpPost = new HttpPost(ArcGisConstants.baseDataUrl + ArcGisConstants.BOX_QUERY_FEATURES);
     6     // 处理请求参数
     7     String where = "mapid=" + "'" + boxVo.getMapId() + "'";
     8     // 创建POST请求参数, 必须用NameValuePair
     9     List<NameValuePair> params = new ArrayList<>();
    10     // 设置传值参数类型为json
    11     params.add(new BasicNameValuePair("f", "json"));
    12     // 将拼装好的条件添加到参数中
    13     params.add(new BasicNameValuePair("where", where));
    14     // 返回所有字段
    15     params.add(new BasicNameValuePair("outFields", "*"));
    16     //设置http Post请求参数
    17     HttpEntity entity = new UrlEncodedFormEntity(params, ArcGisConstants.DEFAULT_CHARSET);
    18     httpPost.setEntity(entity);
    19     // 发送请求
    20     HttpResponse response = httpclient.execute(httpPost);
    21     // 处理结果集
    22     Map<String, Object> oldFeature = new HashMap<>();
    23     if (response.getStatusLine().getStatusCode() == 200) {
    24       // 调用EntityUtils.toString方法最后会自动把inputStream close掉的
    25       String result = EntityUtils.toString(response.getEntity());
    26       LOGGER.info("arcgis查询box: box = " + result);
    27       // 将旧的对象从result解析出来,得到objectid
    28       Map<String, Object> data = new HashMap<>();
    29       data = JsonUtil.jsonString2SimpleObj(result, data.getClass());
    30       // 得到旧的features集合
    31       List<Map<String, Object>> oldFeatures = (List<Map<String, Object>>) data.get(ArcGisConstants.FEATURES);
    32       // 获取第一个数据
    33       if (oldFeatures.size() > 0) {
    34         oldFeature = oldFeatures.get(0);
    35       }
    36     }
    37     // 释放资源
    38     httpclient.close();
    39     return oldFeature;
    40   }

    相关常量类

     1 /**
     2  * ArcGis常量类
     3  *
     4  */
     5 @Component()
     6 public class ArcGisConstants implements InitializingBean {
     7   private static final Logger LOG = java.util.logging.Logger.getLogger(ArcGisConstants.class.getName());
     8 
     9   /*
    10  * 设置请求参数:
    11  *  setConnectionRequestTimeout: 设置从连接池中取连接的超时时间
    12  *  setConnectTimeout: 设置连接超时时间
    13  *  setSocketTimeout: 设置请求超时时间
    14  */
    15   public static final RequestConfig REQUEST_CONFIG = RequestConfig.custom().setConnectionRequestTimeout(1000 * 10).setConnectTimeout(1000 * 10).setSocketTimeout(1000 * 10).build();
    16 
    17   /********** 定义通用常量 **********/
    18   public static final String FEATURES = "features";                         // features
    19   public static final String GEOMETRY = "geometry";                         // 控件模型
    20   public static final String ATTRIBUTES = "attributes";                     // 数据模型
    21   public static final String DEFAULT_CHARSET = "UTF-8";                     // 请求的编码格式
    22   public static final String DEFAULT_TIME_FORMAT = "yyyy-MM-dd";            // 请求的编码格式
    23   public static final String BOX = "box";                                   // 配电箱
    24 
    25   /********** 定义通用字段 **********/
    26   public static final String BOX_ADD_FEATURES = "/0/addFeatures";           // 添加box的url
    27   public static final String BOX_UPDATE_FEATURES = "/0/updateFeatures";     // 更改box的url
    28   public static final String BOX_DELETE_FEATURES = "/0/deleteFeatures";     // 删除box的url
    29   public static final String BOX_QUERY_FEATURES = "/0/query";               // 查询box的url
    30   public static final String OBJECT_ID = "objectid";                        // objectid 主要用于图层要素的更新操作
    31   public static final String BOX_NAME = "boxname";                         // box名称
    32   public static final String BOX_NO = "boxno";                             // box编号
    33   public static final String CREATE_TIME = "createtime";                    // 要素创建时间
    34   public static final String ADDRESS = "address";                           // 安装地址
    35   public static final String PHOTO = "photo";                               // 照片路径
    36   public static final String LON = "x";                                     // 经度
    37   public static final String LAT = "y";                                     // 纬度
    38 
    39 
    40   /********** 根据租户信息获取到的ArcGis数据图层url, 赋值到baseDataUrl **********/
    41   @Value("${portal.url}")
    42   @Getter
    43   @Setter
    44   private String portalUrl;                                                 // 租户url
    45 
    46   public static String baseDataUrl;                                         // 根据租户信息获取到的ArcGis数据图层url
    47 
    48   @Override
    49   public void afterPropertiesSet() throws Exception {
    50     //请求path
    51     String path = "/web/tenant/1";
    52     Map<String, String> headers = new HashMap<>();
    53     //(必填)根据期望的Response内容类型设置
    54     headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_JSON);
    55     Request request = new Request(Method.GET, HttpSchema.HTTP + portalUrl, path, null, null, Constants.DEFAULT_TIMEOUT);
    56     request.setHeaders(headers);
    57     //调用服务端
    58     Response response = Client.execute(request);
    59     if (response.getStatusCode() == 200) {
    60       Map<String, Object> param = new HashMap<>();
    61       param = JsonUtil.jsonString2SimpleObj(response.getBody(), param.getClass());
    62       Map<String, Object> data = (Map<String, Object>) param.get("data");
    63       Map<String, Object> applicationProfile = (Map<String, Object>) data.get("applicationProfileVo");
    64       baseDataUrl = (String) applicationProfile.get("mapUrl");
    65       LOG.info("数据图层为: " + baseDataUrl);
    66     } else {
    67       throw new RuntimeException("初始化ArcGis数据链接失败...");
    68     }
    69   }
    70 }

    相关工具类

      1 /**
      2  * arcGis工具类
      3  * 
      4  */
      5 public class ArcGisUtils {
      6   /**
      7    * 处理照片路径
      8    *
      9    * @param attachments
     10    * @return
     11    */
     12   public static StringBuffer processPhotosAddStr(List<Attachment> attachments, String type) {
     13     StringBuffer photoStr = new StringBuffer();
     14     String devType = "";
     15     if (type.equals(ArcGisConstants.BOX)) {
     16       devType = ArcGisConstants.BOX_PHOTOS;
     17     }
     18     if (attachments != null && attachments.size() > 0) {
     19       for (int i = 0; i <= attachments.size() - 1; i++) {
     20         photoStr.append(attachments.get(i).getAddress().split(devType)[1]);
     21         if (i < attachments.size() - 1) {
     22           // 不是最后一张
     23           photoStr.append(",");
     24         }
     25       }
     26     }
     27     return photoStr;
     28   }
     29 
     30   /**
     31    * 创建新的容器模型
     32    *
     33    * @param geometry
     34    * @param attributes
     35    * @return
     36    */
     37   public static List<Map<String, Object>> createNewFeatures(Map<String, Object> geometry, Map<String, Object> attributes) {
     38     List<Map<String, Object>> features = new ArrayList<>();
     39     HashMap<String, Object> feature = new HashMap<>();
     40     feature.put(ArcGisConstants.GEOMETRY, geometry);
     41     feature.put(ArcGisConstants.ATTRIBUTES, attributes);
     42     features.add(feature);
     43     return features;
     44   }
     45 
     46   /**
     47    * 创建新的空间模型: 根据经纬度
     48    *
     49    * @param lon
     50    * @param lat
     51    * @return
     52    */
     53   public static Map<String, Object> createNewGeometry(BigDecimal lon, BigDecimal lat) {
     54     Map<String, Object> newGeometry = new HashMap<>();
     55     if (lon != null) {
     56       newGeometry.put(ArcGisConstants.LON, lon);
     57     } else {
     58       newGeometry.put(ArcGisConstants.LON, 0);
     59     }
     60     if (lat != null) {
     61       newGeometry.put(ArcGisConstants.LAT, lat);
     62     } else {
     63       newGeometry.put(ArcGisConstants.LAT, 0);
     64     }
     65     return newGeometry;
     66   }
     67 
     68   /**
     69    * 创建新的box数据模型
     70    *
     71    * @param boxVo
     72    * @param objectid
     73    * @return
     74    */
     75   public static Map<String, Object> createNewBoxAttributes(BoxVo boxVo, Integer objectid) {
     76     Map<String, Object> newAttributes = new HashMap<>();
     77     if (objectid != null) {
     78       // 对于修改时的objectid必须进行赋值, 这行代码仅针对于修改box时使用
     79       newAttributes.put(ArcGisConstants.OBJECT_ID, objectid);
     80     } else {
     81       // 对于新增时的createtime进行赋值, 这行代码仅针对于新增box时使用
     82       newAttributes.put(ArcGisConstants.CREATE_TIME, new SimpleDateFormat(ArcGisConstants.DEFAULT_TIME_FORMAT).format(new Date()));
     83     }
     84     newAttributes.put(ArcGisConstants.BOX_NAME, boxVo.getName());
     85     newAttributes.put(ArcGisConstants.BOX_NO, boxVo.getChannelNumber());
     86     newAttributes.put(ArcGisConstants.ADDRESS, boxVo.getInstalledAddress());
     87     newAttributes.put(ArcGisConstants.PHOTO, ArcGisUtils.processPhotosAddStr(boxVo.getAttachments(), ArcGisConstants.BOX));
     88     if (boxVo.getLongitude() != null) {
     89       newAttributes.put(ArcGisConstants.LON, boxVo.getLongitude());
     90     } else {
     91       newAttributes.put(ArcGisConstants.LON, 0);
     92     }
     93     if (boxVo.getLatitude() != null) {
     94       newAttributes.put(ArcGisConstants.LAT, boxVo.getLatitude());
     95     } else {
     96       newAttributes.put(ArcGisConstants.LAT, 0);
     97     }
     98     return newAttributes;
     99   }
    100 }
  • 相关阅读:
    [oracle] linux Oracle 安装配置
    [dns] linux dns 安装配置
    [apache] linux Apache 编译安装
    [yum] linux yum 配置本地和ftp源
    [ftp] linux ftp 安装配置
    [ssh 无密码访问]linux ssh公匙密匙无密码访问
    [php ] linux php 搭建
    [mysql ] linux mysal 修改字符集
    [ mysql ] linux mysql 忘记root密码重置
    国安是冠军
  • 原文地址:https://www.cnblogs.com/yanwu0527/p/8309803.html
Copyright © 2011-2022 走看看