270 lines
11 KiB
Java
270 lines
11 KiB
Java
package pl.wat.ms4ds.terrain.nmt;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import pl.wat.ms4ds.terrain.*;
|
|
|
|
import java.io.*;
|
|
import java.nio.file.FileSystemException;
|
|
import java.util.HashMap;
|
|
import java.util.Set;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipInputStream;
|
|
|
|
public class NMTDataReader {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(NMTDataReader.class);
|
|
|
|
static void main(String[] args) {
|
|
// File dir = new File(System.getProperty("user.home") + "/nmt/gugik_SkorowidzNMT2018.gml");
|
|
|
|
HashMap<Coord.Grid, NMTData> nmtDataHashMap = new HashMap<>();
|
|
|
|
String inDir = "C:/Workspace/nmt/gugik_1m/asc/m-34/";
|
|
String outDir = "D:/work/unzipped/";
|
|
// String testFn = "D:\\Work\\73771_1025306_NMT-M348Dc41.xyz";
|
|
// String testFn = "D:\\Work\\M-33-7-A-c-3-2.asc";
|
|
String testFn = "D:\\Work\\N-34-139-A-b-2-4.asc";
|
|
|
|
// N-34-139-A-b-2-4.asc
|
|
|
|
// try {
|
|
//// readFromFileASC(testFn, nmtDataHashMap);
|
|
// readFromFile(testFn, nmtDataHashMap);
|
|
// } catch (IOException e) {
|
|
// return;
|
|
// }
|
|
// renameFiles(inDir, inDir);
|
|
|
|
|
|
Set<String> files = NMTDataProvider.listFiles(inDir);
|
|
for (String file : files) {
|
|
try {
|
|
String unzipfn = unzipFile(inDir + file, outDir);
|
|
String fpath = outDir + unzipfn;
|
|
readFromFile(fpath, nmtDataHashMap);
|
|
File f = new File(fpath);
|
|
f.delete();
|
|
} catch (IOException _) {
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
static void renameFiles(String inDir, String outDir) {
|
|
try {
|
|
Set<String> fileNames = NMTDataProvider.listFiles(inDir);
|
|
for (String fn : fileNames) {
|
|
String fn1 = fn.substring(0, fn.lastIndexOf("-") + 1);
|
|
String fn2 = fn.substring(fn.lastIndexOf("-") + 1, fn.indexOf("."));
|
|
String ext = fn.substring(fn.indexOf("."));
|
|
int pos;
|
|
if (fn2.length() == 5) {
|
|
pos = 1;
|
|
} else if (fn2.length() == 6) {
|
|
pos = 2;
|
|
} else {
|
|
//if (fn2.length() == 7)
|
|
pos = 3;
|
|
}
|
|
String fn3 = fn2.substring(0, pos);
|
|
String fn4 = fn2.substring(pos, pos + 1);
|
|
String fn5 = fn2.substring(pos + 1, pos + 2);
|
|
String fn6 = fn2.substring(pos + 2, pos + 3);
|
|
String fn7 = fn2.substring(pos + 3, pos + 4);
|
|
String nfn = fn1 + fn3 + '-' + fn4 + '-' + fn5 + '-' + fn6 + '-' + fn7;
|
|
File fileToMove = new File(inDir + fn);
|
|
boolean isMoved = fileToMove.renameTo(new File(outDir + nfn + ext));
|
|
if (!isMoved) {
|
|
throw new FileSystemException(outDir + fileToMove);
|
|
}
|
|
System.out.println(nfn);
|
|
|
|
}
|
|
} catch (Exception e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
|
|
private static void readFromFile(String fn, HashMap<Coord.Grid, NMTData> nmtDataHashMap) throws IOException {
|
|
long start = System.currentTimeMillis();
|
|
File file = new File(fn);
|
|
InputStream inputStream = new FileInputStream(file);
|
|
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
|
|
String line = br.readLine();
|
|
String[] split = line.split(" ");
|
|
if (split.length == 2) {
|
|
// ASC GRID format
|
|
readASC(br, line, nmtDataHashMap);
|
|
} else {
|
|
// split.length ==3
|
|
// XYZ format
|
|
readXYZ(br, line, nmtDataHashMap);
|
|
}
|
|
}
|
|
logger.debug("Time= {}[ms]", System.currentTimeMillis() - start);
|
|
}
|
|
|
|
private static void readASC(BufferedReader br, String firstLine, HashMap<Coord.Grid, NMTData> nmtDataHashMap) throws IOException {
|
|
String line = firstLine;
|
|
String[] split = line.split(" ");
|
|
int ncols = Integer.parseInt(split[1]);
|
|
line = br.readLine();
|
|
split = line.split(" ");
|
|
int nrows = Integer.parseInt(split[1]);
|
|
line = br.readLine();
|
|
split = line.split(" ");
|
|
double xll_puwg = Double.parseDouble(split[1]);
|
|
line = br.readLine();
|
|
split = line.split(" ");
|
|
double yll_puwg = Double.parseDouble(split[1]);
|
|
line = br.readLine();
|
|
split = line.split(" ");
|
|
double cellsize = Double.parseDouble(split[1]);
|
|
line = br.readLine();
|
|
split = line.split(" ");
|
|
double nodata = Double.parseDouble(split[1]);
|
|
double[][] data = new double[nrows][ncols];
|
|
String s;
|
|
for (int i = nrows - 1; i >= 0; i--) {
|
|
line = br.readLine();
|
|
// start od 1, gdyż wiersz zaczyna się od spacji
|
|
int start = 1;
|
|
int end;
|
|
for (int j = 0; j < ncols - 1; j++) {
|
|
end = line.indexOf(' ', start);
|
|
s = line.substring(start, end);
|
|
data[i][j] = Double.parseDouble(s);
|
|
start = end + 1;
|
|
}
|
|
s = line.substring(start);
|
|
data[i][ncols - 1] = Double.parseDouble(s);
|
|
}
|
|
NMTData nmtData = new NMTData(-1, -1, 0, 0);
|
|
Coord.Geo geoCoord = new Coord.Geo();
|
|
Coord.Puwg puwgCoord = new Coord.Puwg();
|
|
double h;
|
|
int x;
|
|
int y;
|
|
double y_puwg = yll_puwg;
|
|
for (int i = 0; i < nrows; i++) {
|
|
double x_puwg = xll_puwg;
|
|
for (int j = 0; j < ncols; j++) {
|
|
h = data[i][j];
|
|
if (h <= nodata) {
|
|
x_puwg += cellsize;
|
|
continue;
|
|
}
|
|
if (nmtData.ell > x_puwg || nmtData.eur < x_puwg || nmtData.nll > y_puwg || nmtData.nur < y_puwg) {
|
|
// Punkt poza granicą bieżącego kwadratu.
|
|
Coord.convertPUWG1992ToWGS84(y_puwg, x_puwg, geoCoord);
|
|
x = Coord.zamienDlugoscGeoNaIdKwadratuX(geoCoord.lon);
|
|
y = Coord.zamienSzerokoscGeoNaIdKwadratuY(geoCoord.lat);
|
|
final int x1 = x;
|
|
final int y1 = y;
|
|
nmtData = nmtDataHashMap.computeIfAbsent(new Coord.Grid(x, y), k -> new NMTData(x1, y1, 0, 0));
|
|
if (nmtData.nur == 0) {
|
|
// Kwadrat jeszcze nie był odczytany (czysty).
|
|
// Współrzędne geo środka kwadratu.
|
|
geoCoord.lon = Coord.zamienIdKwadratuXNaDlugoscGeo(x);
|
|
geoCoord.lat = Coord.zamienIdKwadratuYNaSzerokoscGeo(y);
|
|
// Wyznacz współrzędne PUWG lewego dolnego rogu kwadratu.
|
|
Coord.convertWGS84ToPUWG1992(geoCoord.lat - MapConsts.DELTA_Y / 2, geoCoord.lon - MapConsts.DELTA_X / 2, puwgCoord);
|
|
nmtData.ell = (int) puwgCoord.easting;
|
|
nmtData.nll = (int) puwgCoord.northing;
|
|
// Wyznacz współrzędne PUWG prawego górnego rogu kwadratu.
|
|
Coord.convertWGS84ToPUWG1992(geoCoord.lat + MapConsts.DELTA_Y / 2, geoCoord.lon + MapConsts.DELTA_X / 2, puwgCoord);
|
|
nmtData.eur = (int) puwgCoord.easting;
|
|
nmtData.nur = (int) puwgCoord.northing;
|
|
}
|
|
}
|
|
if (h > nodata) {
|
|
nmtData.sum += h;
|
|
nmtData.count++;
|
|
}
|
|
x_puwg += cellsize;
|
|
}
|
|
y_puwg += cellsize;
|
|
}
|
|
}
|
|
|
|
private static void readXYZ(BufferedReader br, String firstLine, HashMap<Coord.Grid, NMTData> nmtDataHashMap) throws IOException {
|
|
Coord.Puwg puwgCoord = new Coord.Puwg();
|
|
Coord.Geo geo = new Coord.Geo();
|
|
String line = firstLine;
|
|
String[] split;
|
|
double x_puwg;
|
|
double y_puwg;
|
|
double h;
|
|
int x;
|
|
int y;
|
|
NMTData nmtData = new NMTData(-1, -1, 0, 0);
|
|
while (line != null) {
|
|
// start od 0, gdyż nie ma spacji na początku
|
|
int start = 0;
|
|
int end;
|
|
end = line.indexOf(' ', start);
|
|
String s = line.substring(start, end);
|
|
x_puwg = Double.parseDouble(s);
|
|
start = end + 1;
|
|
end = line.indexOf(' ', start);
|
|
s = line.substring(start, end);
|
|
y_puwg = Double.parseDouble(s);
|
|
start = end + 1;
|
|
s = line.substring(start);
|
|
h = Double.parseDouble(s);
|
|
if (nmtData.ell > x_puwg || nmtData.eur < x_puwg || nmtData.nll > y_puwg || nmtData.nur < y_puwg) {
|
|
// Punkt poza granicą bieżącego kwadratu.
|
|
Coord.convertPUWG1992ToWGS84(y_puwg, x_puwg, geo);
|
|
x = Coord.zamienDlugoscGeoNaIdKwadratuX(geo.lon);
|
|
y = Coord.zamienSzerokoscGeoNaIdKwadratuY(geo.lat);
|
|
final int x1 = x;
|
|
final int y1 = y;
|
|
nmtData = nmtDataHashMap.computeIfAbsent(new Coord.Grid(x, y), k -> new NMTData(x1, y1, 0, 0));
|
|
if (nmtData.nur == 0) {
|
|
// Kwadrat jeszcze nie był odczytany (czysty).
|
|
// Współrzędne geo środka kwadratu.
|
|
geo.lon = Coord.zamienIdKwadratuXNaDlugoscGeo(x);
|
|
geo.lat = Coord.zamienIdKwadratuYNaSzerokoscGeo(y);
|
|
// Wyznacz współrzędne PUWG lewego dolnego rogu kwadratu.
|
|
Coord.convertWGS84ToPUWG1992(geo.lat - MapConsts.DELTA_Y / 2, geo.lon - MapConsts.DELTA_X / 2, puwgCoord);
|
|
nmtData.ell = (int) puwgCoord.easting;
|
|
nmtData.nll = (int) puwgCoord.northing;
|
|
// Wyznacz współrzędne PUWG prawego górnego rogu kwadratu.
|
|
Coord.convertWGS84ToPUWG1992(geo.lat + MapConsts.DELTA_Y / 2, geo.lon + MapConsts.DELTA_X / 2, puwgCoord);
|
|
nmtData.eur = (int) puwgCoord.easting;
|
|
nmtData.nur = (int) puwgCoord.northing;
|
|
}
|
|
}
|
|
nmtData.sum += h;
|
|
nmtData.count++;
|
|
line = br.readLine();
|
|
}
|
|
}
|
|
|
|
public static String unzipFile(String zipFileName, String destDir) throws IOException {
|
|
byte[] buffer = new byte[1024];
|
|
String unzipFileName = "";
|
|
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFileName))) {
|
|
ZipEntry zipEntry = zis.getNextEntry();
|
|
while (zipEntry != null) {
|
|
unzipFileName = zipEntry.getName();
|
|
File newFile = new File(destDir + unzipFileName);
|
|
// File newFile = new File(destDir + File.separator + unzipFileName);
|
|
int len;
|
|
// write file content
|
|
FileOutputStream fos = new FileOutputStream(newFile);
|
|
while ((len = zis.read(buffer)) > 0) {
|
|
fos.write(buffer, 0, len);
|
|
}
|
|
fos.close();
|
|
zipEntry = zis.getNextEntry();
|
|
}
|
|
zis.closeEntry();
|
|
}
|
|
return unzipFileName;
|
|
}
|
|
}
|