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();
|
}
|
}
|
|
}
|