diff --git a/.gitignore b/.gitignore index 9e97857..a524c14 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,4 @@ build/ ### VS Code ### .vscode/ /src/main/resources/5963105_www.peteralbus.com.pfx -/src/main/resources/application.yml +/src/main/resources/application.yml.example diff --git a/src/main/java/com/peteralbus/controller/EarthquakeInfoController.java b/src/main/java/com/peteralbus/controller/EarthquakeInfoController.java index 98d7ae4..c17ff05 100644 --- a/src/main/java/com/peteralbus/controller/EarthquakeInfoController.java +++ b/src/main/java/com/peteralbus/controller/EarthquakeInfoController.java @@ -26,26 +26,11 @@ import java.util.Map; @RequestMapping("/earthquakeInfo") public class EarthquakeInfoController { - /** - * The Estimate util. - */ - EstimateUtil estimateUtil; /** * The Earthquake info service. */ EarthquakeInfoService earthquakeInfoService; - /** - * Sets estimate util. - * - * @param estimateUtil the estimate util - */ - @Autowired - public void setEstimateUtil(EstimateUtil estimateUtil) - { - this.estimateUtil = estimateUtil; - } - /** * Sets earthquake info service. * @@ -82,38 +67,6 @@ public class EarthquakeInfoController mapParameter.put("earthquakeId",earthquakeId); return earthquakeInfoService.queryInfoWithLine(mapParameter).get(0); } - - /** - * Gets death predict. - * - * @param earthquakeId the earthquake info id - * @return the death predict - */ - @GetMapping("/getDeathPredict") - public double getDeathPredict(int earthquakeId) - { - Map mapParameter = new HashMap(); - mapParameter.put("earthquakeId",earthquakeId); - EarthquakeInfo earthquakeInfo=earthquakeInfoService.queryInfoWithLine(mapParameter).get(0); - int population=3133; - return estimateUtil.deathPredict(population,earthquakeInfo.getMagnitude(),earthquakeInfo.getHighIntensity()); - } - - /** - * Gets economy predict. - * - * @param earthquakeId the earthquake info id - * @return the economy predict - */ - @GetMapping("/getEconomyPredict") - public double getEconomyPredict(int earthquakeId) - { - Map mapParameter = new HashMap(); - mapParameter.put("earthquakeId",earthquakeId); - EarthquakeInfo earthquakeInfo=earthquakeInfoService.queryInfoWithLine(mapParameter).get(0); - return estimateUtil.economyPredict(earthquakeInfo.getHighIntensity()); - } - /** * Add earthquake string. * diff --git a/src/main/java/com/peteralbus/controller/EstimateController.java b/src/main/java/com/peteralbus/controller/EstimateController.java new file mode 100644 index 0000000..6cd9d2c --- /dev/null +++ b/src/main/java/com/peteralbus/controller/EstimateController.java @@ -0,0 +1,107 @@ +package com.peteralbus.controller; + +import com.peteralbus.entity.Estimate; +import com.peteralbus.entity.EarthquakeInfo; +import com.peteralbus.service.EstimateService; +import com.peteralbus.service.EarthquakeInfoService; +import com.peteralbus.util.EstimateUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.time.LocalDateTime; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +@CrossOrigin +@RestController +@RequestMapping("/estimate") +public class EstimateController { + /** + * The Estimate util. + */ + EstimateUtil estimateUtil; + + EstimateService estimateService; + /** + * The Earthquake info service. + */ + EarthquakeInfoService earthquakeInfoService; + + /** + * Sets earthquake info service. + * + * @param earthquakeInfoService the earthquake info service + */ + @Autowired + public void setEarthquakeInfoService(EarthquakeInfoService earthquakeInfoService) + { + this.earthquakeInfoService = earthquakeInfoService; + } + /** + * Sets estimate util. + * + * @param estimateUtil the estimate util + */ + @Autowired + public void setEstimateUtil(EstimateUtil estimateUtil) + { + this.estimateUtil = estimateUtil; + } + /** + * Sets estimate service. + * + * @param estimateService + */ + @Autowired + public void setAnalyzeService(EstimateService estimateService) { + this.estimateService = estimateService; + } + + /** + * Gets estimate result. + * + * @param earthquakeId the earthquake info id + * @return the estimate result + */ + @GetMapping("/getAnalyzeResult") + public Estimate getPredictResult(long earthquakeId){ + System.out.println(earthquakeId); + int count = estimateService.queryAnalyze(earthquakeId); + Estimate estimate = new Estimate(); + if(count==0){ + Map mapParameter = new HashMap(); + mapParameter.put("earthquakeId",earthquakeId); + EarthquakeInfo earthquakeInfo=earthquakeInfoService.queryInfoWithLine(mapParameter).get(0); + double magnitude = earthquakeInfo.getMagnitude(), + highIntensity = earthquakeInfo.getHighIntensity(), + longitude = earthquakeInfo.getLongitude(), + latitude = earthquakeInfo.getLatitude(), + longRadius = earthquakeInfo.getIntensityLineList().get(2).getLongRadius(), + shortRadius = earthquakeInfo.getIntensityLineList().get(2).getShortRadius(); + System.out.println(longRadius); + LocalDateTime earthquakeTime = earthquakeInfo.getEarthquakeTime(); + double radians = Math.toRadians(latitude);//将角度转换为弧度。 + double minLongitude = longitude-shortRadius/(111-Math.cos(radians)), + maxLongitude = longitude+shortRadius/(111-Math.cos(radians)), + minLatitude = latitude-longRadius/111, + maxLatitude = latitude+longRadius/111; + int population=(int)estimateService.getPopulation(minLongitude,maxLongitude,minLatitude,maxLatitude); + estimate.setPredictDeath(estimateUtil.deathPredict(earthquakeInfo.getEarthquakeId(),population,magnitude,highIntensity,earthquakeTime,longitude,latitude)); + estimate.setPredictEconomy(estimateUtil.economyPredict(earthquakeInfo.getHighIntensity())); + estimate.setEarthquakeId(earthquakeId); + estimate.setPopulation(population); + Date date = new Date(); + estimate.setGmt_time(date); + estimateService.insertAnalyze(estimate); + } + else + { + estimate = estimateService.queryAnalyzeById(earthquakeId); + } + return estimate; + } +} diff --git a/src/main/java/com/peteralbus/entity/Estimate.java b/src/main/java/com/peteralbus/entity/Estimate.java new file mode 100644 index 0000000..5fcd131 --- /dev/null +++ b/src/main/java/com/peteralbus/entity/Estimate.java @@ -0,0 +1,25 @@ +package com.peteralbus.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +import java.util.Date; + +@ToString +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Estimate { + @TableId(type= IdType.ASSIGN_ID) + private Long analyzeId; + private Long earthquakeId; + private Double predictDeath; + private Double predictEconomy; + private int population; + private Date gmt_time; +} diff --git a/src/main/java/com/peteralbus/mapper/EstimateMapper.java b/src/main/java/com/peteralbus/mapper/EstimateMapper.java new file mode 100644 index 0000000..5f3b23e --- /dev/null +++ b/src/main/java/com/peteralbus/mapper/EstimateMapper.java @@ -0,0 +1,14 @@ +package com.peteralbus.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.peteralbus.entity.Estimate; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +@Mapper +public interface EstimateMapper extends BaseMapper +{ + Double getPopulation(@Param("minLongitude") Double minLongitude, @Param("maxLongitude") Double maxLongitude, @Param("minLatitude") Double minLatitude, @Param("maxLatitude") Double maxLatitude); + Estimate queryAnalyzeById(@Param("earthquakeId") long earthquakeId); + int queryAnalyze(@Param("earthquakeId") long earthquakeId); +} diff --git a/src/main/java/com/peteralbus/service/EarthquakeInfoService.java b/src/main/java/com/peteralbus/service/EarthquakeInfoService.java index a11a0b8..38a173c 100644 --- a/src/main/java/com/peteralbus/service/EarthquakeInfoService.java +++ b/src/main/java/com/peteralbus/service/EarthquakeInfoService.java @@ -1,6 +1,7 @@ package com.peteralbus.service; import com.peteralbus.entity.EarthquakeInfo; +import org.apache.ibatis.annotations.Param; import java.util.List; import java.util.Map; diff --git a/src/main/java/com/peteralbus/service/EstimateService.java b/src/main/java/com/peteralbus/service/EstimateService.java new file mode 100644 index 0000000..730538e --- /dev/null +++ b/src/main/java/com/peteralbus/service/EstimateService.java @@ -0,0 +1,20 @@ +package com.peteralbus.service; + +import com.peteralbus.entity.Estimate; +import org.apache.ibatis.annotations.Param; + +public interface EstimateService { + /** + * Get + * + * @param minLongitude the minimum Longitude + * @param maxLongitude the maximum Longitude + * @param minLatitude the minimum Latitude + * @param maxLatitude the maximum Latitude + * @return the double + */ + double getPopulation(@Param("minLongitude") Double minLongitude, @Param("maxLongitude") Double maxLongitude, @Param("minLatitude") Double minLatitude, @Param("maxLatitude") Double maxLatitude); + int insertAnalyze(Estimate estimate); + Estimate queryAnalyzeById(long earthquakeId); + int queryAnalyze(@Param("earthquakeId") long earthquakeId); +} diff --git a/src/main/java/com/peteralbus/service/impl/EstimateServiceImpl.java b/src/main/java/com/peteralbus/service/impl/EstimateServiceImpl.java new file mode 100644 index 0000000..607deb4 --- /dev/null +++ b/src/main/java/com/peteralbus/service/impl/EstimateServiceImpl.java @@ -0,0 +1,41 @@ +package com.peteralbus.service.impl; + +import com.peteralbus.entity.Estimate; +import com.peteralbus.mapper.EstimateMapper; +import com.peteralbus.service.EstimateService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class EstimateServiceImpl implements EstimateService { + + EstimateMapper estimateMapper; + @Autowired + public void setAnalyzeMapper(EstimateMapper estimateMapper){ + this.estimateMapper = estimateMapper; + } + @Override + public double getPopulation(Double minLongitude, Double maxLongitude, Double minLatitude, Double maxLatitude) { + return estimateMapper.getPopulation(minLongitude,maxLongitude,minLatitude,maxLatitude); + } + @Override + public int insertAnalyze(Estimate analyze){ + int result = 0; + result = estimateMapper.insert(analyze); + if (result == 0) + { + return result; + } + return result; + } + + @Override + public Estimate queryAnalyzeById(long earthquakeId) { + return estimateMapper.queryAnalyzeById(earthquakeId); + } + + @Override + public int queryAnalyze(long earthquakeId) { + return estimateMapper.queryAnalyze(earthquakeId); + } +} diff --git a/src/main/java/com/peteralbus/util/EstimateUtil.java b/src/main/java/com/peteralbus/util/EstimateUtil.java index c27f029..0c1cf59 100644 --- a/src/main/java/com/peteralbus/util/EstimateUtil.java +++ b/src/main/java/com/peteralbus/util/EstimateUtil.java @@ -2,6 +2,9 @@ package com.peteralbus.util; import org.springframework.stereotype.Component; +import java.time.LocalDateTime; +import java.util.Date; + /** * The type Estimate util. * @author PeterAlbus @@ -14,20 +17,40 @@ public class EstimateUtil * * @param population the population * @param magnitude the magnitude + * @param earthquakeTime the time of earthquake + * @param longitude the longitude + * @param latitude the latitude * @return the double */ - public double deathPredict(int population,double magnitude,double intensity) + public double deathPredict(Long earthquake_id,int population, double magnitude, double intensity, LocalDateTime earthquakeTime,Double longitude,Double latitude) { double deathPredict = 0; /*M为震级 I为震中强度*/ - double mCoefficient = Math.abs((magnitude-4.17)/0.35*intensity-0.97); + double mCoefficient = Math.abs((magnitude-4.17)/(0.35*intensity-0.97)); /*population 为人口密度*/ double denCoefficient = 0.05*Math.log(population)+0.74; - /*白天为1,夜晚为0.75*/ - double timeCoefficient = 0.75; + /*时间修正系数*/ + /* 1:00-5:59为2,6:00-8:59为1,9:00-19:59为5/9,20:00-00:59为5/3 */ + int hour = earthquakeTime.getHour(); + double timeCoefficient = 5.0/3.0; + if(hour>=1 && hour<6){ + timeCoefficient = 2.0; + } + else if(hour>=6 && hour < 9){ + timeCoefficient = 1.0; + } + else if(hour >= 9 && hour < 20){ + timeCoefficient = 5.0/9.0; + } /*西部强度修正系数 0.3661*/ - double strengthCoefficient = 0.4853; - double bdr = 0.5; + double temp_latitude = 0.85 * longitude -64.4;/*黑河-腾冲现模拟方程*/ + double strengthCoefficient = 0.3661; + if(latitude < temp_latitude){ + strengthCoefficient = 0.4853; + } + double bdr = (intensity*17.205-101.861)*0.01; + if(bdr <= 0) + bdr = 0.01; deathPredict = 0.461*mCoefficient*denCoefficient*timeCoefficient*strengthCoefficient*Math.exp(12.285*bdr); return deathPredict; // double deathPredict = 0; @@ -42,8 +65,7 @@ public class EstimateUtil * @return the double */ public double economyPredict(double highIntensity) - { - return Math.pow(10,0.84444*highIntensity-1.831)/10000; + {return Math.pow(10,0.84444*highIntensity-1.831)/10000; } } diff --git a/src/main/resources/mapper/EarthquakeInfoMapper.xml b/src/main/resources/mapper/EarthquakeInfoMapper.xml index 9dcc848..5d93025 100644 --- a/src/main/resources/mapper/EarthquakeInfoMapper.xml +++ b/src/main/resources/mapper/EarthquakeInfoMapper.xml @@ -30,4 +30,9 @@ limit #{startIndex},#{pageSize} + + diff --git a/src/main/resources/mapper/EstimateMapper.xml b/src/main/resources/mapper/EstimateMapper.xml new file mode 100644 index 0000000..2975726 --- /dev/null +++ b/src/main/resources/mapper/EstimateMapper.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +