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