Newer
Older
task-bldg / src / main / java / osm / surveyor / task / city / DataLoader.java
  1. package osm.surveyor.task.city;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.boot.CommandLineRunner;
  6. import org.springframework.http.HttpMethod;
  7. import org.springframework.http.HttpStatus;
  8. import org.springframework.http.ResponseEntity;
  9. import org.springframework.stereotype.Component;
  10. import org.springframework.web.client.RestTemplate;
  11.  
  12. import com.fasterxml.jackson.databind.JsonNode;
  13. import com.fasterxml.jackson.databind.ObjectMapper;
  14.  
  15. import lombok.RequiredArgsConstructor;
  16. import osm.surveyor.task.city.model.CitiesJson;
  17. import osm.surveyor.task.city.model.City;
  18. import osm.surveyor.task.city.model.CityJson;
  19. import osm.surveyor.task.city.model.Status;
  20. import osm.surveyor.task.mesh.CitymeshRepository;
  21. import osm.surveyor.task.mesh.model.Citymesh;
  22. import osm.surveyor.task.task.TaskService;
  23. import osm.surveyor.task.task.model.TaskEntity;
  24. import osm.surveyor.task.util.Geojson;
  25. import osm.surveyor.task.util.JsonFeature;
  26. import osm.surveyor.task.util.JsonGeometryLine;
  27. import osm.surveyor.task.util.JsonGeometryPoint;
  28. import osm.surveyor.task.util.JsonProperties;
  29. import osm.surveyor.task.util.Point;
  30.  
  31. @RequiredArgsConstructor
  32. @Component
  33. public class DataLoader implements CommandLineRunner {
  34. private final CityRepository cityRepository;
  35. private final CitymeshRepository meshRepository;
  36. private final TaskService taskService;
  37. private final CityService cityService;
  38.  
  39. String url = "https://surveyor.mydns.jp/osm-data";
  40. @Override
  41. public void run(String... args) throws Exception {
  42. // 「src/main/resources/static/city/index.json」を読み込む
  43. String body = httpGet(url + "/index.json");
  44. CitiesJson pojo = new ObjectMapper().readValue(body, CitiesJson.class);
  45. String site = pojo.getSite();
  46. for (CityJson citiesJson : pojo.getList()) {
  47. City city = new City();
  48. city.setSite(site);
  49. city.setCitycode(citiesJson.getCode());
  50. city.setCityname(citiesJson.getName());
  51. city.setFolder(citiesJson.getPath());
  52. Status status = citiesJson.getStatus();
  53. if (status != null) {
  54. city.setStatus(status);
  55. }
  56.  
  57. Point coordinates = citiesJson.toCoordinates();
  58. city.setLng(coordinates.getLng());
  59. city.setLat(coordinates.getLat());
  60.  
  61. cityRepository.save(city);
  62. storeTask(city);
  63. cityService.updateStatus(city.getCitycode());
  64. }
  65. }
  66. private String httpGet(String url) throws Exception {
  67. RestTemplate restTemplate = new RestTemplate();
  68. System.out.println(String.format("INFO: httpGet(%s)", url));
  69. ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
  70. HttpStatus httpStatus = response.getStatusCode();
  71. if (httpStatus.isError()) {
  72. System.out.println("ERROE: Can not access '"+ url +"'");
  73. throw new Exception("ERROE: Can not access '"+ url +"'");
  74. }
  75. String body = response.getBody();
  76. return body;
  77. }
  78. private void storeTask(City city) throws Exception {
  79. String folder = city.getFolder();
  80. ObjectMapper mapper = new ObjectMapper();
  81. JsonNode node = mapper.readTree(httpGet(url + "/" + folder + "/bldg/index.geojson"));
  82. Geojson geojson = new Geojson();
  83. geojson.parse(node);
  84. List<JsonFeature> features = geojson.getFeatures();
  85. for (JsonFeature f : features) {
  86. JsonProperties prop = f.getProperties();
  87. if (prop != null) {
  88. String meshcode = prop.getId();
  89. if (meshcode != null) {
  90. // メッシュの中心点
  91. JsonGeometryPoint geometryPoint = f.getGeometryPoint();
  92. if (geometryPoint != null) {
  93. Citymesh mesh = new Citymesh();
  94. mesh.setCitycode(city.getCitycode());
  95. mesh.setMeshcode(meshcode);
  96. mesh.setVersion(prop.getVersion());
  97. mesh.setSurveyYear(prop.getSurveyYear());
  98. mesh.setPath(prop.getPath());
  99. mesh.setPoint(geometryPoint.getPoint());
  100. mesh.setCity(city);
  101. TaskEntity task = (TaskEntity) taskService.getTaskByMesh(city.getCitycode(), meshcode);
  102. if (task == null) {
  103. Status status = city.getStatus();
  104. if (status != null) {
  105. mesh.setStatus(status);
  106. }
  107. }
  108. else {
  109. mesh.setStatus(task.getStatus());
  110. mesh.setUsername(task.getUsername());
  111. }
  112. meshRepository.save(mesh);
  113. }
  114. // メッシュの範囲
  115. // [制限事項] 'index.geojson' 内の配列は必ず、LINE_STRINGフィーチャの前にPOINTフィーチャが存在していなければならない
  116. JsonGeometryLine geometryLine = f.getGeometryLine();
  117. if (geometryLine != null) {
  118. Citymesh mesh = new Citymesh();
  119. mesh.setCitycode(city.getCitycode());
  120. mesh.setMeshcode(meshcode);
  121. mesh.setLine(geometryLine);
  122. mesh.setPoint(geometryLine.getCenter());
  123. mesh.setCity(city);
  124. // [制限事項]
  125. Citymesh meshDb = (Citymesh) meshRepository.findOne(city.getCitycode(), meshcode);
  126. if (meshDb != null) {
  127. mesh.setVersion(meshDb.getVersion());
  128. mesh.setSurveyYear(meshDb.getSurveyYear());
  129. mesh.setPath(meshDb.getPath());
  130. mesh.setLng(meshDb.getLng());
  131. mesh.setLat(meshDb.getLat());
  132. mesh.setStatus(meshDb.getStatus());
  133. mesh.setUsername(meshDb.getUsername());
  134. mesh.setStatus(meshDb.getStatus());
  135. }
  136. meshRepository.save(mesh);
  137. }
  138. }
  139. }
  140. }
  141. }
  142. }