# Conflicts:
#	pom.xml
#	src/main/java/org/ccalm/weather/AirTemperature.java
#	src/main/java/org/ccalm/weather/Precipitation.java
#	src/main/java/org/ccalm/weather/SoilTmperature.java
#	src/main/resources/logback-spring.xml
This commit is contained in:
Igor I
2025-01-05 09:24:42 +05:00
8 changed files with 495 additions and 378 deletions

View File

@ -5,11 +5,7 @@ import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
@ -53,20 +49,97 @@ import ucar.nc2.dataset.NetcdfDataset;
@Controller
public class AirTemperature implements ServletContextAware {
@Value("${custom.config.db_url}")
private String db_url;
@Value("${custom.config.db_login}")
private String db_login;
@Value("${custom.config.db_password}")
private String db_password;
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(AirTemperature.class);
@Value("${custom.config.db_all.url}")
private String db_url_all;
@Value("${custom.config.db_all.login}")
private String db_login_all;
@Value("${custom.config.db_all.password}")
private String db_password_all;
@Value("${custom.config.db_ru.url}")
private String db_url_ru;
@Value("${custom.config.db_ru.login}")
private String db_login_ru;
@Value("${custom.config.db_ru.password}")
private String db_password_ru;
@Value("${custom.config.data_dir}")
private String data_dir;
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SoilTmperature.class);
private ServletContext context;
private jakarta.servlet.ServletContext context;
@Override
public void setServletContext(jakarta.servlet.ServletContext servletContext) {
this.context = servletContext;
}
//---------------------------------------------------------------------------
public String getCountryId(Statement st,double lon,double lat) {
String country_id = "";
try {
String sql = "select c.id from main.countries c where del=false and ST_Contains(c.geom,ST_SetSRID(st_makepoint(" + lon + "," + lat + "),4326)) limit 1";
try (ResultSet rs = st.executeQuery(sql)) {
if (rs.next()) {
country_id = rs.getString(1);
}
}
} catch (SQLException ex) {
logger.error("N9: " + ex.getMessage(), ex);
}
return country_id;
}
//---------------------------------------------------------------------------
public String getPointId(Statement st, String country_id, double lon, double lat) {
String point_id = "";
PreparedStatement pstmt = null;
try {
String sql = "SELECT id FROM main.points WHERE lon = ? AND lat = ? LIMIT 1";
pstmt = st.getConnection().prepareStatement(sql);
pstmt.setDouble(1, lon);
pstmt.setDouble(2, lat);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
point_id = rs.getString(1);
}
}
} catch (SQLException ex) {
logger.error("N9: " + ex.getMessage(), ex);
}
if(point_id.isEmpty()){
try {
String sql = "insert into main.points(country_id,geom,lon,lat)values(?,ST_SetSRID(st_makepoint(?,?),4326),?,?) RETURNING id";
pstmt = st.getConnection().prepareStatement(sql);
pstmt.setLong(1, Long.valueOf(country_id));
pstmt.setDouble(2, lon);
pstmt.setDouble(3, lat);
pstmt.setDouble(4, lon);
pstmt.setDouble(5, lat);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
point_id = rs.getString(1); // Получаем ID, возвращаемый SQL-запросом
}
}
} catch (SQLException ex) {
logger.error("N11: " + ex.getMessage(), ex);
}
}
return point_id;
}
//---------------------------------------------------------------------------
public Connection getConn(String url, String login,String password){
Connection conn = null;
try{
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(url,login,password);
}catch(Exception ex)
{
logger.error("N1: "+ex.getMessage()+"<br>",ex);
}
return conn;
}
//---------------------------------------------------------------------------
/**
* This function is run every day from CRON, to see the settings call the function: "sudo crontab -e -u tomcat" on PC 127.0.0.1
* @param date - If the field is empty, it is filled in automatically with the current day (When running from CRON, this field is empty)
@ -86,24 +159,8 @@ public class AirTemperature implements ServletContextAware {
if (!dir.exists()) dir.mkdirs();
//response.getWriter().append("Served at: ").append(request.getContextPath());
Connection conn = null;
try{
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(db_url,db_login,db_password);
if(conn!=null)
{
logger.info("Connect is OK!");
result+="Connect is OK!<br>";
}else
{
logger.info("<br>Connect is ERROR<br>");
result+="Connect is ERROR!<br>";
}
}catch(Exception ex)
{
logger.error("N1:"+ex.getMessage()+"<br>",ex);
result+="Connect Exception:"+ex.getMessage()+"<br>";
}
Connection conn_all = getConn(db_url_all,db_login_all,db_password_all);
Connection conn_ru = getConn(db_url_ru,db_login_ru,db_password_ru);
//Example request: http://ccalm.org/AirTemperature?date=20210531
//Example request: http://localhost:8080/AirTemperature?date=20210531
@ -229,15 +286,19 @@ public class AirTemperature implements ServletContextAware {
}
dimIt = null;
Statement st=null;
Statement st_all=null;
Statement st_ru=null;
try {
st = conn.createStatement();
if(conn_all!=null) st_all = conn_all.createStatement();
if(conn_ru!=null) st_ru = conn_ru.createStatement();
} catch (SQLException ex) {
logger.error("N3:"+ex.getMessage(),ex);
}
try {
st.executeUpdate("BEGIN TRANSACTION;");
String sql = "BEGIN TRANSACTION;";
if(st_all!=null) st_all.executeUpdate(sql);
if(st_ru!=null) st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N4:"+ex.getMessage(),ex);
}
@ -245,35 +306,41 @@ public class AirTemperature implements ServletContextAware {
result+="Size="+dataArrayLat.getSize()+"<br>";
//Delete old data
logger.info("Delete old data 1");
/*logger.info("Delete old data 1");
try {
String sql="delete from main.air_temperature where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast;
st.executeUpdate(sql);
if(st_all!=null) st_all.executeUpdate(sql);
if(st_ru!=null) st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N5:"+ex.getMessage(),ex);
}
}*/
logger.info("Delete old data 2");
try {
String sql="delete from main.air_temperature_dates where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast;
st.executeUpdate(sql);
if(st_all!=null) st_all.executeUpdate(sql);
if(st_ru!=null) st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N6:"+ex.getMessage(),ex);
}
logger.info("Delete old data 3");
try {
String sql="delete from main.air_temperature where date<=CURRENT_DATE-'730 days'::INTERVAL";
st.executeUpdate(sql);
//String sql="delete from main.air_temperature where date<=CURRENT_DATE-'730 days'::INTERVAL";
String sql="delete from main.air_temperature_dates where date<=CURRENT_DATE-'730 days'::INTERVAL";
if(st_all!=null) st_all.executeUpdate(sql);
if(st_ru!=null) st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N7:"+ex.getMessage(),ex);
}
try {
st.executeUpdate("END TRANSACTION;");
if(st_all!=null) st_all.executeUpdate("END TRANSACTION;");
if(st_ru!=null) st_ru.executeUpdate("END TRANSACTION;");
} catch (SQLException ex) {
logger.error("N7.1:"+ex.getMessage(),ex);
}
try {
st.executeUpdate("BEGIN TRANSACTION;");
if(st_all!=null) st_all.executeUpdate("BEGIN TRANSACTION;");
if(st_ru!=null) st_ru.executeUpdate("BEGIN TRANSACTION;");
} catch (SQLException ex) {
logger.error("N7.2:"+ex.getMessage(),ex);
}
@ -294,29 +361,30 @@ public class AirTemperature implements ServletContextAware {
if(!Float.isNaN(dataArrayTmp.getFloat(pos))) //On the water none temperatyre.
{
String country_id="";
ResultSet rs=null;
try {
String sql="select c.id from main.countries c where ST_Contains(c.geom,ST_SetSRID(st_makepoint("+lon+","+lat+"),4326)) limit 1";
rs = st.executeQuery(sql);
} catch (SQLException ex) {
logger.error("N8:"+ex.getMessage(),ex);
boolean db_all=false,db_ru=false;
if(st_all!=null && country_id.isEmpty()) {
country_id = getCountryId(st_all, lon, lat);
if(!country_id.isEmpty()) db_all=true;
}
if (rs != null) {
try {
if (rs.next())
country_id=rs.getString(1);
rs.close();
} catch (SQLException ex) {
logger.error("N9:"+ex.getMessage(),ex);
}
if(st_ru!=null && country_id.isEmpty()) {
country_id = getCountryId(st_ru, lon, lat);
if(!country_id.isEmpty()) db_ru=true;
}
if(country_id!=null && !country_id.equals("") && !country_id.equals("null"))
{
//logger.info(lon + "," + lat +","+dataArrayTmp.getFloat(pos));
try {
String sql="insert into main.air_temperature(date,hours,val,geom,country_id,air_temperature_date_id)values(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+","+dataArrayTmp.getFloat(pos)+",ST_SetSRID(st_makepoint("+lon+","+lat+"),4326),"+country_id+",main.get_air_temperature_date(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+"));";
st.executeUpdate(sql);
String point_id="";
if(db_all) {
point_id = getPointId(st_all, country_id, lon, lat);
String sql="insert into main.air_temperature(val,point_id,country_id,air_temperature_date_id)values("+dataArrayTmp.getFloat(pos)+","+point_id+","+country_id+",main.get_air_temperature_date(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+"));";
st_all.executeUpdate(sql);
}
if(db_ru) {
point_id = getPointId(st_ru, country_id, lon, lat);
String sql="insert into main.air_temperature(val,point_id,country_id,air_temperature_date_id)values("+dataArrayTmp.getFloat(pos)+","+point_id+","+country_id+",main.get_air_temperature_date(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+"));";
st_ru.executeUpdate(sql);
}
} catch (SQLException ex) {
logger.error("N10:"+ex.getMessage(),ex);
}
@ -330,41 +398,42 @@ public class AirTemperature implements ServletContextAware {
//Cut data piece from big country of Russia
try {
String sql="update main.air_temperature w set country_id=null where country_id=7 and not ST_Contains(ST_SetSRID(ST_GeomFromText('POLYGON((10.00 66.00,10.00 40.00,179.00 40.00,179.00 66.00,10.00 66.00))'),4326),w.geom);";
st.executeUpdate(sql);
String sql="update main.points w set country_id=null where country_id=7 and not ST_Contains(ST_SetSRID(ST_GeomFromText('POLYGON((10.00 66.00,10.00 40.00,179.00 40.00,179.00 66.00,10.00 66.00))'),4326),w.geom);";
if(st_all!=null) st_all.executeUpdate(sql);
if(st_ru!=null) st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N11:"+ex.getMessage(),ex);
}
//Delete values where country_id is null
try {
String sql="delete from main.air_temperature where country_id is null;";
st.executeUpdate(sql);
String sql="delete from main.points where country_id is null;";
if(st_all!=null) st_all.executeUpdate(sql);
if(st_ru!=null) st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N12:"+ex.getMessage(),ex);
}
try {
st.executeUpdate("END TRANSACTION;");
String sql = "END TRANSACTION;";
if(st_all!=null) st_all.executeUpdate(sql);
if(st_ru!=null) st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N13:"+ex.getMessage(),ex);
}
gid.close();
} catch (IOException ex) {
logger.error("N14:"+ex.getMessage(),ex);
}
try {conn.close();} catch (SQLException ex) {logger.error("N15:"+ex.getMessage(),ex);}
try { if(conn_all!=null) conn_all.close(); } catch (SQLException ex) {logger.error("N15:"+ex.getMessage(),ex);}
try { if(conn_ru!=null) conn_ru.close();} catch (SQLException ex) {logger.error("N15:"+ex.getMessage(),ex);}
result+="End!<br>";
return result;
}
//---------------------------------------------------------------------------
@Override
public void setServletContext(ServletContext context) {
this.context=context;
}
//---------------------------------------------------------------------------
public static String CutBeforeFirst(StringBuffer str,String ch)
{
@ -393,29 +462,14 @@ public class AirTemperature implements ServletContextAware {
boolean error=false;
String result="";
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(db_url, db_login, db_password);
if (conn != null) {
logger.info("Connect is OK!");
} else {
error=true;
result="An error occurred while connecting to the database!";
}
} catch (Exception ex) {
logger.error("N16:"+ex.getMessage(),ex);
error=true;
result="<br>SQLException: "+ex.getMessage()+"<br>";
}
Connection conn_all = getConn(db_url_all,db_login_all,db_password_all);
if(!error)
{
Statement st;
Statement st_all;
try {
st = conn.createStatement();
//String sql = "SELECT to_char(date, 'YYYY-MM-DD') as date,hours as hour,EXTRACT(DAY FROM CURRENT_DATE-date) as day FROM main.air_temperature group by date,hours order by date,hours;";
st_all = conn_all.createStatement();
String sql = """
SELECT
to_char(date, 'YYYY-MM-DD') as date,
@ -426,7 +480,7 @@ public class AirTemperature implements ServletContextAware {
group by date,hours
order by date,hours
""";
ResultSet rs = st.executeQuery(sql);
ResultSet rs = st_all.executeQuery(sql);
if(rs!=null)
{
boolean exists=false;
@ -448,15 +502,17 @@ public class AirTemperature implements ServletContextAware {
result="[]";
}
}
st.close();
conn.close();
st_all.close();
conn_all.close();
} catch (SQLException ex) {
result="<br>SQLException:"+ex.getMessage()+"<br>";
logger.error("N18:"+ex.getMessage(),ex);
}
}finally{
try { conn_all.close(); } catch (SQLException ex) { logger.error("N19: "+ex.getMessage(),ex); }
}
}
return result;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
}

View File

@ -18,13 +18,14 @@ import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ -53,22 +54,88 @@ import ucar.nc2.dataset.NetcdfDataset;
@Controller
public class Precipitation implements ServletContextAware {
private static final Logger logger = LogManager.getLogger(Precipitation.class);
@Value("${custom.config.db_url}")
private String db_url;
@Value("${custom.config.db_login}")
private String db_login;
@Value("${custom.config.db_password}")
private String db_password;
@Value("${custom.config.db_all.url}")
private String db_url_all;
@Value("${custom.config.db_all.login}")
private String db_login_all;
@Value("${custom.config.db_all.password}")
private String db_password_all;
@Value("${custom.config.db_ru.url}")
private String db_url_ru;
@Value("${custom.config.db_ru.login}")
private String db_login_ru;
@Value("${custom.config.db_ru.password}")
private String db_password_ru;
@Value("${custom.config.data_dir}")
private String data_dir;
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Precipitation.class);
private ServletContext context;
//http://127.0.0.1:8080/AirTemperature
private jakarta.servlet.ServletContext context;
//---------------------------------------------------------------------------
@Override
public void setServletContext(jakarta.servlet.ServletContext servletContext) {
this.context=servletContext;
}
//---------------------------------------------------------------------------
public static String CutBeforeFirst(StringBuffer str,String ch)
{
int pos=str.indexOf(ch);
String result="";
if(pos==-1)
{
result.concat(str.toString());
str.delete(0,str.length());
}else
{
result=str.substring(0,pos);
str.delete(0,pos+1);
}
return result;
}
//---------------------------------------------------------------------------
public String getCountryId(Statement st,double lon,double lat) {
String country_id = "";
ResultSet rs = null;
try {
rs = st.executeQuery("select c.id from main.countries c where del=false and ST_Contains(c.geom,ST_SetSRID(st_makepoint(" + lon + "," + lat + "),4326)) limit 1");
} catch (SQLException ex) {
logger.error("N9: " + ex.getMessage(), ex);
}
if (rs != null) {
try {
if (rs.next())
country_id = rs.getString(1);
rs.close();
} catch (SQLException ex) {
logger.error("N10: " + ex.getMessage(), ex);
}
}
return country_id;
}
//---------------------------------------------------------------------------
public Connection getConn(String url, String login,String password){
Connection conn = null;
try{
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(url,login,password);
}catch(Exception ex)
{
logger.error("N1: "+ex.getMessage()+"<br>",ex);
}
return conn;
}
//---------------------------------------------------------------------------
/**
* Example http://127.0.0.1:8080/AirTemperature
* @param response
* @param date
* @return
*/
@RequestMapping(value = "/geodatalist/Precipitation",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
@ResponseBody
public Object ajaxTamer(HttpServletResponse response,@RequestParam(required=false,name="date") String date) {
@ -85,24 +152,8 @@ public class Precipitation implements ServletContextAware {
if (!dir.exists()) dir.mkdirs();
//response.getWriter().append("Served at: ").append(request.getContextPath());
Connection conn = null;
try{
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(db_url,db_login,db_password);
if(conn!=null)
{
logger.info("Connect is OK!");
result+="Connect is OK!<br>";
}else
{
logger.info("<br>Connect is ERROR<br>");
result+="Connect is ERROR!<br>";
}
}catch(Exception e)
{
logger.error("N1:"+e.getMessage()+"<br>",e);
result+="Connect Exception:"+e.getMessage()+"<br>";
}
Connection conn_all = getConn(db_url_all,db_login_all,db_password_all);
Connection conn_ru = getConn(db_url_ru,db_login_ru,db_password_ru);
//Example request: http://localhost:8080/Precipitation?date=20210531
if(date==null || date.equals(""))
@ -225,15 +276,18 @@ public class Precipitation implements ServletContextAware {
}
dimIt = null;
Statement st=null;
Statement st_all=null;
Statement st_ru=null;
try {
st = conn.createStatement();
st_all = conn_all.createStatement();
st_ru = conn_ru.createStatement();
} catch (SQLException ex) {
logger.error("N3:"+ex.getMessage(),ex);
}
try {
st.executeUpdate("BEGIN TRANSACTION;");
st_all.executeUpdate("BEGIN TRANSACTION;");
st_ru.executeUpdate("BEGIN TRANSACTION;");
} catch (SQLException ex) {
logger.error("N4:"+ex.getMessage(),ex);
}
@ -244,11 +298,28 @@ public class Precipitation implements ServletContextAware {
logger.info("Delete old data 1");
try {
String sql="delete from main.precipitation where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast;
st.executeUpdate(sql);
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N5:"+ex.getMessage(),ex);
}
logger.info("Delete old data 2");
try {
String sql="delete from main.precipitation_dates where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast;
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N6:"+ex.getMessage(),ex);
}
logger.info("Delete old data 3");
try {
String sql="delete from main.precipitation_dates where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast;
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.info(ex.getMessage());
}
System.out.println("Delete old data 3");
try {
String sql="delete from main.precipitation_dates where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast;
st.executeUpdate(sql);
@ -258,18 +329,21 @@ public class Precipitation implements ServletContextAware {
logger.info("Delete old data 3");
try {
String sql="delete from main.precipitation where date<=CURRENT_DATE-'730 days'::INTERVAL";
st.executeUpdate(sql);
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N7:"+ex.getMessage(),ex);
}
try {
st.executeUpdate("END TRANSACTION;");
st_all.executeUpdate("END TRANSACTION;");
st_ru.executeUpdate("END TRANSACTION;");
} catch (SQLException ex) {
logger.error("N7.1:"+ex.getMessage(),ex);
}
try {
st.executeUpdate("BEGIN TRANSACTION;");
st_all.executeUpdate("BEGIN TRANSACTION;");
st_ru.executeUpdate("BEGIN TRANSACTION;");
} catch (SQLException ex) {
logger.error("N7.2:"+ex.getMessage(),ex);
}
@ -290,29 +364,28 @@ public class Precipitation implements ServletContextAware {
if(!Float.isNaN(dataArrayTmp.getFloat(pos))) //On the water none temperatyre.
{
String country_id="";
ResultSet rs=null;
try {
rs = st.executeQuery("select c.id from main.countries c where ST_Contains(c.geom,ST_SetSRID(st_makepoint("+lon+","+lat+"),4326)) limit 1");
} catch (SQLException ex) {
logger.error("N8:"+ex.getMessage(),ex);
boolean db_all=false,db_ru=false;
if(country_id.isEmpty()) {
country_id = getCountryId(st_all, lon, lat);
if(!country_id.isEmpty()) db_all=true;
}
if (rs != null) {
try {
if (rs.next())
country_id=rs.getString(1);
rs.close();
} catch (SQLException ex) {
logger.error("N9:"+ex.getMessage(),ex);
}
if(country_id.isEmpty()) {
country_id = getCountryId(st_ru, lon, lat);
if(!country_id.isEmpty()) db_ru=true;
}
if(country_id!=null && !country_id.equals("") && !country_id.equals("null"))
{
//logger.info(lon + "," + lat +","+dataArrayTmp.getFloat(pos));
try {
String sql="insert into main.precipitation(date,hours,val,geom,country_id,precipitation_date_id)values(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+","+dataArrayTmp.getFloat(pos)+",ST_SetSRID(st_makepoint("+lon+","+lat+"),4326),"+country_id+",main.get_precipitation_date(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+"));";
st.executeUpdate(sql);
if(db_all)
st_all.executeUpdate(sql);
if(db_ru)
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N10:"+ex.getMessage(),ex);
throw new Exception("Failed insert precipitation...");
}
}
@ -325,7 +398,8 @@ public class Precipitation implements ServletContextAware {
//Cut data piece from big country of Russia
try {
String sql="update main.precipitation w set country_id=null where country_id=7 and not ST_Contains(ST_SetSRID(ST_GeomFromText('POLYGON((10.00 66.00,10.00 40.00,179.00 40.00,179.00 66.00,10.00 66.00))'),4326),w.geom);";
st.executeUpdate(sql);
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N11:"+ex.getMessage(),ex);
}
@ -333,13 +407,16 @@ public class Precipitation implements ServletContextAware {
//Delete values where country_id is null
try {
String sql="delete from main.precipitation where country_id is null;";
st.executeUpdate(sql);
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N12:"+ex.getMessage(),ex);
}
try {
st.executeUpdate("END TRANSACTION;");
String sql="END TRANSACTION;";
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N13:"+ex.getMessage(),ex);
}
@ -347,34 +424,16 @@ public class Precipitation implements ServletContextAware {
gid.close();
} catch (IOException ex) {
logger.error("N14:"+ex.getMessage(),ex);
}
} catch (Exception ex) {
logger.error("N15: "+ex.getMessage(),ex);
}
try {conn.close();} catch (SQLException ex) {logger.info(ex.getMessage(),ex);}
try {conn_all.close();} catch (SQLException ex) {logger.error("N16: "+ex.getMessage(),ex);}
try {conn_ru.close();} catch (SQLException ex) {logger.error("N16: "+ex.getMessage(),ex);}
result+="End!<br>";
return result;
}
//---------------------------------------------------------------------------
@Override
public void setServletContext(ServletContext context) {
this.context=context;
}
//---------------------------------------------------------------------------
public static String CutBeforeFirst(StringBuffer str,String ch)
{
int pos=str.indexOf(ch);
String result="";
if(pos==-1)
{
result.concat(str.toString());
str.delete(0,str.length());
}else
{
result=str.substring(0,pos);
str.delete(0,pos+1);
}
return result;
}
//---------------------------------------------------------------------------
//List of "Air temperature" dates from database in JSON
@CrossOrigin
@ -388,28 +447,13 @@ public class Precipitation implements ServletContextAware {
String result="";
//Load DB configuration from "config.xml"
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(db_url, db_login, db_password);
if (conn != null) {
logger.info("Connect is OK!");
} else {
error=true;
result="An error occurred while connecting to the database!";
}
} catch (Exception ex) {
logger.error("N15:"+ex.getMessage(),ex);
error=true;
result="<br>SQLException: "+ex.getMessage()+"<br>";
}
Connection conn_all = getConn(db_url_all,db_login_all,db_password_all);
if(!error)
{
Statement st;
Statement st_all=null;
try {
st = conn.createStatement();
//String sql = "SELECT to_char(date, 'YYYY-MM-DD') as date,hours as hour,DATE_PART('doy',date)-1 as day FROM main.precipitation group by date,hours order by date,hours";
st_all = conn_all.createStatement();
String sql = """
SELECT
to_char(date, 'YYYY-MM-DD') as date,
@ -421,8 +465,8 @@ public class Precipitation implements ServletContextAware {
date,
hours
order by date,hours
""";
ResultSet rs = st.executeQuery(sql);
""";
ResultSet rs = st_all.executeQuery(sql);
if(rs!=null)
{
boolean exists=false;
@ -444,13 +488,14 @@ public class Precipitation implements ServletContextAware {
result="[]";
}
}
st.close();
conn.close();
st_all.close();
} catch (SQLException ex) {
result="<br>SQLException:"+ex.getMessage()+"<br>";
logger.error("N17:"+ex.getMessage(),ex);
}
}
}finally{
try { conn_all.close(); } catch (SQLException ex) { logger.error("N18: "+ex.getMessage(),ex); }
}
}
return result;
}
//---------------------------------------------------------------------------

View File

@ -53,22 +53,89 @@ import ucar.nc2.dataset.NetcdfDataset;
@Controller
public class SoilTmperature implements ServletContextAware {
@Value("${custom.config.db_url}")
private String db_url;
@Value("${custom.config.db_login}")
private String db_login;
@Value("${custom.config.db_password}")
private String db_password;
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SoilTmperature.class);
@Value("${custom.config.db_all.url}")
private String db_url_all;
@Value("${custom.config.db_all.login}")
private String db_login_all;
@Value("${custom.config.db_all.password}")
private String db_password_all;
@Value("${custom.config.db_ru.url}")
private String db_url_ru;
@Value("${custom.config.db_ru.login}")
private String db_login_ru;
@Value("${custom.config.db_ru.password}")
private String db_password_ru;
@Value("${custom.config.data_dir}")
private String data_dir;
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SoilTmperature.class);
private ServletContext context;
//Example: http://127.0.0.1:8081/geodatalist/DownloadSoil?forecast=000
private ServletContext context;
//---------------------------------------------------------------------------
@Override
public void setServletContext(jakarta.servlet.ServletContext servletContext) {
this.context=context;
}
//---------------------------------------------------------------------------
public Connection getConn(String url, String login,String password){
Connection conn = null;
try{
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(url,login,password);
}catch(Exception ex)
{
logger.error("N1: "+ex.getMessage()+"<br>",ex);
}
return conn;
}
//---------------------------------------------------------------------------
public String getCountryId(Statement st,double lon,double lat) {
String country_id = "";
ResultSet rs = null;
try {
rs = st.executeQuery("select c.id from main.countries c where del=false and ST_Contains(c.geom,ST_SetSRID(st_makepoint(" + lon + "," + lat + "),4326)) limit 1");
} catch (SQLException ex) {
logger.error("N9: " + ex.getMessage(), ex);
}
if (rs != null) {
try {
if (rs.next())
country_id = rs.getString(1);
rs.close();
} catch (SQLException ex) {
logger.error("N10: " + ex.getMessage(), ex);
}
}
return country_id;
}
//---------------------------------------------------------------------------
public static String CutBeforeFirst(StringBuffer str,String ch)
{
int pos=str.indexOf(ch);
String result="";
if(pos==-1)
{
result.concat(str.toString());
str.delete(0,str.length());
}else
{
result=str.substring(0,pos);
str.delete(0,pos+1);
}
return result;
}
//---------------------------------------------------------------------------
/**
* Example: http://127.0.0.1:8081/geodatalist/DownloadSoil?forecast=000
* @param response
* @param forecast
* @param date
* @return
*/
@RequestMapping(value = "/geodatalist/DownloadSoil",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
@ResponseBody
public Object ajaxTamer(HttpServletResponse response,@RequestParam(required=true,name="forecast") String forecast,@RequestParam(required=false,name="date") String date) {
@ -88,24 +155,9 @@ public class SoilTmperature implements ServletContextAware {
if (!dir.exists()) dir.mkdirs();
//response.getWriter().append("Served at: ").append(request.getContextPath());
Connection conn = null;
try{
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(db_url,db_login,db_password);
if(conn!=null)
{
logger.info("Connect is OK!");
result+="Connect is OK!<br>";
}else
{
logger.info("<br>Connect is ERROR<br>");
result+="Connect is ERROR!<br>";
}
}catch(Exception ex)
{
logger.error("N1: "+ex.getMessage()+"<br>",ex);
result+="Connect Exception:"+ex.getMessage()+"<br>";
}
Connection conn_all = getConn(db_url_all,db_login_all,db_password_all);
Connection conn_ru = getConn(db_url_ru,db_login_ru,db_password_ru);
//Example request: http://ccalm.org/DownloadWeather?forecast=000&date=20210531
//Example request: http://localhost:8080/CCALM/DownloadWeather?forecast=000
@ -241,15 +293,18 @@ public class SoilTmperature implements ServletContextAware {
}
dimIt = null;
Statement st=null;
Statement st_all=null;
Statement st_ru=null;
try {
st = conn.createStatement();
st_all = conn_all.createStatement();
st_ru = conn_all.createStatement();
} catch (SQLException ex) {
logger.error("N3: "+ex.getMessage(),ex);
}
try {
st.executeUpdate("BEGIN TRANSACTION;");
st_all.executeUpdate("BEGIN TRANSACTION;");
st_ru.executeUpdate("BEGIN TRANSACTION;");
} catch (SQLException ex) {
logger.error("N4: "+ex.getMessage(),ex);
}
@ -260,21 +315,24 @@ public class SoilTmperature implements ServletContextAware {
logger.info("Delete old data 1");
try {
String sql="delete from main.soil_temperature where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast;
st.executeUpdate(sql);
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N5: "+ex.getMessage(),ex);
}
logger.info("Delete old data 2");
try {
String sql="delete from main.soil_temperature_dates where date=cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone) and hours="+forecast;
st.executeUpdate(sql);
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N6: "+ex.getMessage(),ex);
}
logger.info("Delete old data 3");
try {
String sql="delete from main.soil_temperature where date<=CURRENT_DATE-'730 days'::INTERVAL";
st.executeUpdate(sql);
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N7: "+ex.getMessage(),ex);
}
@ -282,19 +340,22 @@ public class SoilTmperature implements ServletContextAware {
logger.info("Delete old data 4");
try {
String sql="delete from main.soil_temperature where hours="+forecast;
st.executeUpdate(sql);
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N8: "+ex.getMessage(),ex);
}
}
try {
st.executeUpdate("END TRANSACTION;");
st_all.executeUpdate("END TRANSACTION;");
st_ru.executeUpdate("END TRANSACTION;");
} catch (SQLException ex) {
logger.error("N8.1:"+ex.getMessage(),ex);
}
try {
st.executeUpdate("BEGIN TRANSACTION;");
st_all.executeUpdate("BEGIN TRANSACTION;");
st_ru.executeUpdate("BEGIN TRANSACTION;");
} catch (SQLException ex) {
logger.error("N8.2:"+ex.getMessage(),ex);
}
@ -315,29 +376,24 @@ public class SoilTmperature implements ServletContextAware {
if(!Float.isNaN(dataArrayTmp.getFloat(pos))) //On the water none temperatyre.
{
String country_id="";
ResultSet rs=null;
try {
rs = st.executeQuery("select c.id from main.countries c where ST_Contains(c.geom,ST_SetSRID(st_makepoint("+lon+","+lat+"),4326)) limit 1");
} catch (SQLException ex) {
logger.error("N9: "+ex.getMessage(),ex);
throw new Exception("Failed to select country...");
boolean db_all=false,db_ru=false;
if(country_id.isEmpty()) {
country_id = getCountryId(st_all, lon, lat);
if(!country_id.isEmpty()) db_all=true;
}
if (rs != null) {
try {
if (rs.next())
country_id=rs.getString(1);
rs.close();
} catch (SQLException ex) {
logger.error("N10: "+ex.getMessage(),ex);
throw new Exception("Failed to select country...");
}
if(country_id.isEmpty()) {
country_id = getCountryId(st_ru, lon, lat);
if(!country_id.isEmpty()) db_ru=true;
}
if(country_id!=null && !country_id.equals("") && !country_id.equals("null"))
if(!country_id.isEmpty() && !country_id.equals("null"))
{
//logger.info(lon + "," + lat +","+dataArrayTmp.getFloat(pos));
try {
String sql="insert into main.soil_temperature(date,hours,val,geom,country_id,soil_temperature_date_id)values(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+","+dataArrayTmp.getFloat(pos)+",ST_SetSRID(st_makepoint("+lon+","+lat+"),4326),"+country_id+",main.get_soil_temperature_date(cast(to_timestamp('"+date+" "+time+"', 'YYYYMMDD HH24') as timestamp without time zone),"+forecast+"));";
st.executeUpdate(sql);
if(db_all)
st_all.executeUpdate(sql);
if(db_ru)
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N11: "+ex.getMessage(),ex);
throw new Exception("Failed insert soil temperature...");
@ -353,7 +409,8 @@ public class SoilTmperature implements ServletContextAware {
//Cut data piece from big country of Russia
try {
String sql="update main.soil_temperature w set country_id=null where country_id=7 and not ST_Contains(ST_SetSRID(ST_GeomFromText('POLYGON((10.00 66.00,10.00 40.00,179.00 40.00,179.00 66.00,10.00 66.00))'),4326),w.geom);";
st.executeUpdate(sql);
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N12: "+ex.getMessage(),ex);
}
@ -361,13 +418,16 @@ public class SoilTmperature implements ServletContextAware {
//Delete values where country_id is null
try {
String sql="delete from main.soil_temperature where country_id is null;";
st.executeUpdate(sql);
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N13: "+ex.getMessage(),ex);
}
try {
st.executeUpdate("END TRANSACTION;");
String sql="END TRANSACTION;";
st_all.executeUpdate(sql);
st_ru.executeUpdate(sql);
} catch (SQLException ex) {
logger.error("N14: "+ex.getMessage(),ex);
}
@ -375,37 +435,16 @@ public class SoilTmperature implements ServletContextAware {
gid.close();
} catch (IOException ex) {
logger.error("N15: "+ex.getMessage(),ex);
}
catch (Exception ex) {
} catch (Exception ex) {
logger.error("N16: "+ex.getMessage(),ex);
}
try {conn.close();} catch (SQLException ex) {logger.error("N16: "+ex.getMessage(),ex);}
try {conn_all.close();} catch (SQLException ex) {logger.error("N16: "+ex.getMessage(),ex);}
try {conn_ru.close();} catch (SQLException ex) {logger.error("N16: "+ex.getMessage(),ex);}
result+="End!<br>";
return result;
}
//---------------------------------------------------------------------------
@Override
public void setServletContext(ServletContext context) {
this.context=context;
}
//---------------------------------------------------------------------------
public static String CutBeforeFirst(StringBuffer str,String ch)
{
int pos=str.indexOf(ch);
String result="";
if(pos==-1)
{
result.concat(str.toString());
str.delete(0,str.length());
}else
{
result=str.substring(0,pos);
str.delete(0,pos+1);
}
return result;
}
//---------------------------------------------------------------------------
//List of "Soil temperature" dates from database in JSON
@CrossOrigin
@ -417,28 +456,15 @@ public class SoilTmperature implements ServletContextAware {
boolean error=false;
String result="";
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(db_url, db_login, db_password);
if (conn != null) {
logger.info("Connect is OK!");
} else {
error=true;
result="An error occurred while connecting to the database!";
}
} catch (Exception ex) {
logger.error("N17: "+ex.getMessage(),ex);
error=true;
result="<br>SQLException: "+ex.getMessage()+"<br>";
}
Connection conn_all = getConn(db_url_all,db_login_all,db_password_all);
//Connection conn_ru = getConn(db_url_ru,db_login_ru,db_password_ru);
if(!error)
{
Statement st;
try {
st = conn.createStatement();
st = conn_all.createStatement();
//String sql = "SELECT to_char(date, 'YYYY-MM-DD') as date,hours as hour,EXTRACT(DAY FROM CURRENT_DATE-date) as day FROM main.soil_temperature group by date,hours order by date,hours";
String sql = """
SELECT
@ -475,14 +501,16 @@ public class SoilTmperature implements ServletContextAware {
}
st.close();
conn.close();
} catch (SQLException ex) {
logger.error("N19: "+ex.getMessage(),ex);
result="<br>SQLException:"+ex.getMessage()+"<br>";
}
}finally {
if(conn_all!=null) try{ conn_all.close(); } catch (SQLException ignored){}
}
}
return result;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
}