GeoQueryUtil.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package com.fdkankan.geo;
  2. import lombok.Data;
  3. import lombok.Getter;
  4. import lombok.Setter;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.InitializingBean;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  10. import org.springframework.boot.context.properties.ConfigurationProperties;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.core.io.ClassPathResource;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.util.ResourceUtils;
  15. import javax.annotation.PostConstruct;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.Serializable;
  20. import java.nio.file.Files;
  21. import java.nio.file.Paths;
  22. import java.nio.file.StandardCopyOption;
  23. /**
  24. * @author Xiewj
  25. * @date 2024/3/15
  26. */
  27. @Slf4j
  28. @Getter
  29. @Setter
  30. @Configuration(value = "GeoQueryUtil")
  31. @ConditionalOnProperty(prefix = "geoquery",name = "dataFilePath")
  32. @ConfigurationProperties(prefix = "geoquery")
  33. public class GeoQueryUtil {
  34. @Value("${geoquery.dataFilePath:/mnt/geoQuery/GeoJSON.json}")
  35. private String dataFilePath;
  36. @Value("${geoquery.saveWkbsFilePath:/mnt/geoQuery/GeoJSON.wkbs}")
  37. private String saveWkbsFilePath;
  38. @PostConstruct
  39. public void init() {
  40. File dataFile = new File(dataFilePath);
  41. if (!dataFile.exists()) {
  42. if (!dataFile.getParentFile().exists()){
  43. dataFile.getParentFile().mkdirs();
  44. }
  45. // 从资源文件中读取数据
  46. try {
  47. ClassPathResource classPathResource = new ClassPathResource("GeoJSON.json");
  48. InputStream is = classPathResource.getInputStream();
  49. Files.copy(is, Paths.get(dataFile.getPath()), StandardCopyOption.REPLACE_EXISTING);
  50. log.info("File copied from resources to dataFilePath.");
  51. } catch (IOException e) {
  52. log.error("Error copying file from resources: " + e.getMessage());
  53. }
  54. }
  55. File saveWkbsFile = new File(saveWkbsFilePath);
  56. if (!saveWkbsFile.exists()) {
  57. if (!saveWkbsFile.getParentFile().exists()){
  58. saveWkbsFile.getParentFile().mkdirs();
  59. }
  60. // 从资源文件中读取数据
  61. try {
  62. ClassPathResource classPathResource = new ClassPathResource("GeoJSON.wkbs");
  63. InputStream is = classPathResource.getInputStream();
  64. Files.copy(is, Paths.get(saveWkbsFile.getPath()), StandardCopyOption.REPLACE_EXISTING);
  65. log.info("File copied from resources to saveWkbsFilePath.");
  66. } catch (IOException e) {
  67. log.error("Error copying file from resources: " + e.getMessage());
  68. }
  69. }
  70. AreaCityQuery.Init_StoreInWkbsFile(dataFilePath, saveWkbsFilePath, true);
  71. System.out.println(AreaCityQuery.GetInitInfo().toString()); //打印初始化详细信息,包括性能信息
  72. }
  73. public boolean queryPoint(String location) {
  74. String[] split = location.split(",");
  75. if (split.length<2){
  76. return false;
  77. }
  78. double lng= Double.parseDouble(split[0]);
  79. double lat= Double.parseDouble(split[1]);
  80. AreaCityQuery.QueryResult res1= null;
  81. try {
  82. res1 = AreaCityQuery.QueryPoint(lng, lat, null, null);
  83. } catch (Exception e) {
  84. throw new RuntimeException(e);
  85. }
  86. if (res1!=null &&res1.Result.size()>0){
  87. log.info("查询结果:{},{}",true,res1);
  88. return true;
  89. }else {
  90. log.info("查询结果:{},{}",false,res1);
  91. return false;
  92. }
  93. }
  94. public boolean queryPoint(Double lng,Double lat) {
  95. AreaCityQuery.QueryResult res1= null;
  96. try {
  97. res1 = AreaCityQuery.QueryPoint(lng, lat, null, null);
  98. } catch (Exception e) {
  99. throw new RuntimeException(e);
  100. }
  101. if (res1!=null &&res1.Result.size()>0){
  102. log.info("查询结果:{},{}",true,res1);
  103. return true;
  104. }else {
  105. log.info("查询结果:{},{}",false,res1);
  106. return false;
  107. }
  108. }
  109. public AreaCityQuery.QueryResult queryPointInfo(String location) {
  110. String[] split = location.split(",");
  111. if (split.length<2){
  112. throw new RuntimeException("非法地理坐标");
  113. }
  114. double lng= Double.parseDouble(split[0]);
  115. double lat= Double.parseDouble(split[1]);
  116. AreaCityQuery.QueryResult res1= null;
  117. try {
  118. res1 = AreaCityQuery.QueryPoint(lng, lat, null, null);
  119. } catch (Exception e) {
  120. throw new RuntimeException(e);
  121. }
  122. return res1;
  123. }
  124. public AreaCityQuery.QueryResult queryPointInfo(Double lng,Double lat) {
  125. AreaCityQuery.QueryResult res1= null;
  126. try {
  127. res1 = AreaCityQuery.QueryPoint(lng, lat, null, null);
  128. } catch (Exception e) {
  129. throw new RuntimeException(e);
  130. }
  131. return res1;
  132. }
  133. public static void main(String[] args) throws Exception {
  134. GeoQueryUtil queryUtil = new GeoQueryUtil();
  135. queryUtil.init();
  136. AreaCityQuery.QueryResult queryResult = queryUtil.queryPointInfo("120.885676,23.544815");
  137. System.out.println(queryResult);
  138. }
  139. }