Newer
Older
task-bldg / src / main / java / osm / surveyor / task / city / DataLoader.java
@haya4 haya4 on 6 Aug 2022 3 KB fix : remove iquery.js
  1. package osm.surveyor.task.city;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.List;
  6.  
  7. import org.springframework.boot.CommandLineRunner;
  8. import org.springframework.core.io.ClassPathResource;
  9. import org.springframework.stereotype.Component;
  10.  
  11. import com.fasterxml.jackson.databind.JsonNode;
  12. import com.fasterxml.jackson.databind.ObjectMapper;
  13.  
  14. import lombok.RequiredArgsConstructor;
  15. import osm.surveyor.task.city.model.CitiesJson;
  16. import osm.surveyor.task.city.model.City;
  17. import osm.surveyor.task.city.model.CityJson;
  18. import osm.surveyor.task.city.model.Citymesh;
  19. import osm.surveyor.task.city.model.Status;
  20. import osm.surveyor.task.city.model.Task;
  21. import osm.surveyor.task.util.Geojson;
  22. import osm.surveyor.task.util.JsonFeature;
  23. import osm.surveyor.task.util.JsonGeometryPoint;
  24. import osm.surveyor.task.util.JsonProperties;
  25. import osm.surveyor.task.util.Point;
  26.  
  27. @RequiredArgsConstructor
  28. @Component
  29. public class DataLoader implements CommandLineRunner {
  30. private final CityRepository cityRepository;
  31. private final CitymeshRepository meshRepository;
  32. private final TaskService taskService;
  33. @Override
  34. public void run(String... args) throws Exception {
  35. // 「src/main/resources/static/city/index.json」を読み込む
  36. try (InputStream is = new ClassPathResource("static/city/index.json").getInputStream()) {
  37. CitiesJson pojo = new ObjectMapper().readValue(is, CitiesJson.class);
  38. String site = pojo.getSite();
  39. for (CityJson citiesJson : pojo.getList()) {
  40. City city = new City();
  41. city.setSite(site);
  42. city.setCitycode(citiesJson.getCode());
  43. city.setCityname(citiesJson.getName());
  44. city.setFolder(citiesJson.getPath());
  45. Status status = citiesJson.getStatus();
  46. if (status != null) {
  47. city.setStatus(status);
  48. }
  49.  
  50. Point coordinates = citiesJson.toCoordinates();
  51. city.setLng(coordinates.getLng());
  52. city.setLat(coordinates.getLat());
  53.  
  54. cityRepository.save(city);
  55. storeTask(city);
  56. }
  57. }
  58. }
  59. private void storeTask(City city) throws IOException {
  60. String folder = city.getFolder();
  61. String path = "static/city/"+ folder +"/bldg/index.geojson";
  62. try (InputStream is = new ClassPathResource(path).getInputStream()) {
  63. ObjectMapper mapper = new ObjectMapper();
  64. JsonNode node = mapper.readTree(is);
  65. Geojson geojson = new Geojson();
  66. geojson.parse(node);
  67. List<JsonFeature> features = geojson.getFeatures();
  68. for (JsonFeature f : features) {
  69. JsonGeometryPoint geometryPoint = f.getGeometryPoint();
  70. if (geometryPoint != null) {
  71. if (geometryPoint.getType().equals("Point")) {
  72. JsonProperties prop = f.getProperties();
  73. if (prop != null) {
  74. String meshcode = prop.getId();
  75. if (meshcode != null) {
  76. Citymesh mesh = new Citymesh();
  77. mesh.setCitycode(city.getCitycode());
  78. mesh.setMeshcode(meshcode);
  79. mesh.setVersion(prop.getVersion());
  80. mesh.setPath(prop.getPath());
  81. mesh.setPoint(geometryPoint.getCoordinates().toString());
  82. mesh.setCity(city);
  83. Task task = taskService.getTaskByMesh(city.getCitycode(), meshcode);
  84. if (task == null) {
  85. Status status = city.getStatus();
  86. if (status != null) {
  87. mesh.setStatus(status);
  88. }
  89. }
  90. else {
  91. mesh.setStatus(task.getStatus());
  92. }
  93. meshRepository.save(mesh);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }