guoshilong
2023-06-14 97c752f75880ff6b799b146ea45fefc2c1d59fa5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package cn.gistack.sm.geoJson.controller;
 
import cn.gistack.sm.geoJson.entity.TownsGeoJson;
import cn.gistack.sm.geoJson.service.ITownsGeoJsonService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
 
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @PROJECT_NAME: skjcmanager
 * @DESCRIPTION:
 * @USER: aix
 * @DATE: 2023/6/13 19:50
 */
@Slf4j
@Api(tags="乡镇geoJson数据")
@RestController
@RequestMapping("/town/geoJson")
@AllArgsConstructor
public class TownsGeoJsonController extends BladeController {
 
    private ITownsGeoJsonService townsGeoJsonService;
 
    @GetMapping(value = "/findGeoJsonByCode")
    public R findGeoJsonByCode(@ApiIgnore @RequestParam Map<String, Object> obj) {
        QueryWrapper<TownsGeoJson> queryWrapper = new QueryWrapper<>();
        if (null == obj.get("level") || null == obj.get("code"))
            return R.fail("参数不正确");
        String level = obj.get("level").toString();
        String code = obj.get("code").toString();
 
        Map<String, Object> ret = new HashMap<>();
        ret.put("type", "FeatureCollection");
 
        if (level.equals("1")) {
 
            ret.put("features", townsGeoJsonService.list());
        } else if (level.equals("2")) {
            code = code.substring(0,4);
            ret.put("features", townsGeoJsonService.list(queryWrapper.lambda().likeRight(TownsGeoJson::getCityId,code)));
        } else if (level.equals("3")) {
            code = code.substring(0,6);
            ret.put("features", townsGeoJsonService.list(queryWrapper.lambda().likeRight(TownsGeoJson::getCountyId,code)));
        } else {
            ret.put("features", townsGeoJsonService.list(queryWrapper.lambda().eq(TownsGeoJson::getAreaId,code)));
        }
        return R.data(ret);
    }
 
    /**
     * 本地初始化执行一次就可以
     * @return
     */
//    @GetMapping(value = "/initData")
    public R queryPageList() {
        initData();
        return R.data(200, null, "初始化成功");
    }
 
    private void initData() {
 
        //解析JSON文件的内容
        try {
            // 流读
            File file=new File("D:\\汇图信息\\湖北水利研究院\\开发文档\\geojson\\towns.json");
            FileReader fr=new FileReader(file);
            char[] cbuf=new char[5];
            int len;
            StringBuffer sb = new StringBuffer();
            while((len=fr.read(cbuf))!=-1){
                String str=new String(cbuf,0,len);
                sb.append(str);
            }
            // 关闭流
            fr.close();
 
            // 开始操作json
            JSONObject obj = JSONObject.parseObject(sb.toString());
//            //删除除features以外其它对象,方便下面所需的geojson字符串
            obj.remove("type");
            obj.remove("name");
            obj.remove("crs");
            JSONArray array = obj.getJSONArray("features");
            for (int i = 0; i < array.size(); i++) {
                JSONObject feature = array.getJSONObject(i);
                TownsGeoJson townsGeoJson = new TownsGeoJson();
                townsGeoJson.setName(feature.getJSONObject("properties").get("NAME").toString());
                townsGeoJson.setAreaId(feature.getJSONObject("properties").get("area_id").toString());
                townsGeoJson.setCityId(feature.getJSONObject("properties").get("city_id").toString());
                townsGeoJson.setCityName(feature.getJSONObject("properties").get("city_nm").toString());
                townsGeoJson.setCountyId(feature.getJSONObject("properties").get("county_id").toString());
                townsGeoJson.setCountyName(feature.getJSONObject("properties").get("county_nm").toString());
                townsGeoJson.setGeometry(feature.getJSONObject("geometry"));
                townsGeoJson.setProperties(feature.getJSONObject("properties"));
                townsGeoJsonService.save(townsGeoJson);
            }
 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
}