|
@@ -0,0 +1,117 @@
|
|
|
|
+package com.fdkankan.geo;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import lombok.Data;
|
|
|
|
+import lombok.Getter;
|
|
|
|
+import lombok.Setter;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.beans.factory.InitializingBean;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
|
+import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.util.ResourceUtils;
|
|
|
|
+
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.io.Serializable;
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
+import java.nio.file.Paths;
|
|
|
|
+import java.nio.file.StandardCopyOption;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author Xiewj
|
|
|
|
+ * @date 2024/3/15
|
|
|
|
+ */
|
|
|
|
+@Slf4j
|
|
|
|
+@Getter
|
|
|
|
+@Setter
|
|
|
|
+@Configuration(value = "GeoQueryUtil")
|
|
|
|
+@ConditionalOnProperty(prefix = "geoquery",name = "dataFilePath")
|
|
|
|
+@ConfigurationProperties(prefix = "geoquery")
|
|
|
|
+public class GeoQueryUtil {
|
|
|
|
+ @Value("${geoquery.dataFilePath:/mnt/geoQuery/GeoJSON.json}")
|
|
|
|
+ private String dataFilePath;
|
|
|
|
+
|
|
|
|
+ @Value("${geoquery.saveWkbsFilePath:/mnt/geoQuery/GeoJSON.wkbs}")
|
|
|
|
+ private String saveWkbsFilePath;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @PostConstruct
|
|
|
|
+ void init() {
|
|
|
|
+ File dataFile = new File(dataFilePath);
|
|
|
|
+ if (!dataFile.exists()) {
|
|
|
|
+ if (!dataFile.getParentFile().exists()){
|
|
|
|
+ dataFile.getParentFile().mkdirs();
|
|
|
|
+ }
|
|
|
|
+ // 从资源文件中读取数据
|
|
|
|
+ try {
|
|
|
|
+ ClassPathResource classPathResource = new ClassPathResource("GeoJSON.json");
|
|
|
|
+ InputStream is = classPathResource.getInputStream();
|
|
|
|
+ Files.copy(is, Paths.get(dataFile.getPath()), StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
+ log.info("File copied from resources to dataFilePath.");
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("Error copying file from resources: " + e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ File saveWkbsFile = new File(saveWkbsFilePath);
|
|
|
|
+ if (!saveWkbsFile.exists()) {
|
|
|
|
+ if (!saveWkbsFile.getParentFile().exists()){
|
|
|
|
+ saveWkbsFile.getParentFile().mkdirs();
|
|
|
|
+ }
|
|
|
|
+ // 从资源文件中读取数据
|
|
|
|
+ try {
|
|
|
|
+ ClassPathResource classPathResource = new ClassPathResource("GeoJSON.wkbs");
|
|
|
|
+ InputStream is = classPathResource.getInputStream();
|
|
|
|
+ Files.copy(is, Paths.get(saveWkbsFile.getPath()), StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
+ log.info("File copied from resources to saveWkbsFilePath.");
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("Error copying file from resources: " + e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ AreaCityQuery.Init_StoreInWkbsFile(dataFilePath, saveWkbsFilePath, true);
|
|
|
|
+ System.out.println(AreaCityQuery.GetInitInfo().toString()); //打印初始化详细信息,包括性能信息
|
|
|
|
+ }
|
|
|
|
+ public boolean queryPoint(String location) throws Exception {
|
|
|
|
+ String[] split = location.split(",");
|
|
|
|
+ if (split.length<2){
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ double lng= Double.parseDouble(split[0]);
|
|
|
|
+ double lat= Double.parseDouble(split[1]);
|
|
|
|
+ AreaCityQuery.QueryResult res1=AreaCityQuery.QueryPoint(lng, lat, null, null);
|
|
|
|
+ if (res1!=null &&res1.Result.size()>0){
|
|
|
|
+ log.info("查询结果:{},{}",true,res1);
|
|
|
|
+ return true;
|
|
|
|
+ }else {
|
|
|
|
+ log.info("查询结果:{},{}",false,res1);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ public boolean queryPoint(Double lng,Double lat) throws Exception {
|
|
|
|
+ AreaCityQuery.QueryResult res1=AreaCityQuery.QueryPoint(lng, lat, null, null);
|
|
|
|
+ if (res1!=null &&res1.Result.size()>0){
|
|
|
|
+ log.info("查询结果:{},{}",true,res1);
|
|
|
|
+ return true;
|
|
|
|
+ }else {
|
|
|
|
+ log.info("查询结果:{},{}",false,res1);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
|
+ GeoQueryUtil queryUtil = new GeoQueryUtil();
|
|
|
|
+ System.out.println( queryUtil.queryPoint("109.47237681083061,30.265446034566555"));
|
|
|
|
+ System.out.println( queryUtil. queryPoint("134.345421050937,34.090524661738"));
|
|
|
|
+ }
|
|
|
|
+}
|