Zasadniczy wsad inicjalizacyjny.
This commit is contained in:
215
src/main/java/pl/wat/ms4ds/terenfunkcje/nmt/NMTDataProvider.java
Normal file
215
src/main/java/pl/wat/ms4ds/terenfunkcje/nmt/NMTDataProvider.java
Normal file
@@ -0,0 +1,215 @@
|
||||
package pl.wat.ms4ds.terenfunkcje.nmt;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamConstants;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import java.io.*;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
public class NMTDataProvider {
|
||||
private static final Logger LOGGER = Logger.getLogger(NMTDataProvider.class);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// File dir = new File(System.getProperty("user.home") + "/nmt/gugik_SkorowidzNMT2018.gml");
|
||||
// HashMap<String, String> map = new HashMap<>();
|
||||
// String fn0 = "D:/nmt/gugik_SkorowidzNMT20";
|
||||
// for (int i = 18; i < 26; i++) {
|
||||
// readFileLinksFromGUGiKxml(fn0 + i + ".gml", map);
|
||||
// }
|
||||
// saveLinks("D:/nmt/gugik_links.txt", map);
|
||||
|
||||
// start = 580-730, start=1091-1230
|
||||
//
|
||||
// String dir = "C:/Workspace/nmt/gugik_1m/";
|
||||
// String links_fn = "C:/Workspace/nmt/gugik_links.txt";
|
||||
String dir = args[0];
|
||||
String links_fn = args[1];
|
||||
// int start = Integer.parseInt(args[2]);
|
||||
// int end = Integer.parseInt(args[3]);
|
||||
|
||||
Set<String> files = listFiles("C:/Workspace/nmt/gugik_1m/");
|
||||
|
||||
// downloadFileSet(links_fn, start, end, dir);
|
||||
|
||||
// ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
|
||||
// executor.execute(() -> {
|
||||
// try {
|
||||
// downloadFileSet(links_fn, 290, 730, dir);
|
||||
// } catch (IOException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
// });
|
||||
// executor.execute(() -> {
|
||||
// try {
|
||||
// downloadFileSet(links_fn, 779, 1230, dir);
|
||||
// } catch (IOException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
// });
|
||||
// executor.shutdown();
|
||||
// executor.awaitTermination(25, TimeUnit.MINUTES);
|
||||
LOGGER.info("Koniec");
|
||||
}
|
||||
|
||||
private static void saveLinks(String fn, HashMap<String, String> map) throws IOException {
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(fn));
|
||||
for (String val : map.values()) {
|
||||
writer.write(val);
|
||||
writer.newLine();
|
||||
}
|
||||
writer.close();
|
||||
}
|
||||
|
||||
private static void downloadFile2(String urlStr, String dirName) throws IOException {
|
||||
String fn = urlStr.substring(urlStr.lastIndexOf('_') + 1);
|
||||
String path = dirName + fn;
|
||||
// URI uri = URI.create("https://opendata.geoportal.gov.pl/NumDaneWys/NMT/81441/81441_1643337_N-34-125-D-d-3-1.asc");
|
||||
URI uri = URI.create(urlStr);
|
||||
InputStream inputStream = uri.toURL().openStream();
|
||||
ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream);
|
||||
FileOutputStream fos = new FileOutputStream(path);
|
||||
FileChannel fc = fos.getChannel();
|
||||
fc.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
|
||||
fc.close();
|
||||
fos.close();
|
||||
}
|
||||
|
||||
public static Set<String> listFiles(String dir) {
|
||||
return Stream.of(new File(dir).listFiles())
|
||||
.filter(file -> !file.isDirectory())
|
||||
.map(File::getName)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private static void downloadFile(String urlStr, String dirName) throws IOException {
|
||||
String fn = urlStr.substring(urlStr.lastIndexOf('_') + 1);
|
||||
String path = dirName + fn;
|
||||
URI uri = URI.create(urlStr);
|
||||
try (InputStream inputStream = uri.toURL().openStream();) {
|
||||
BufferedInputStream bis = new BufferedInputStream(inputStream);
|
||||
FileOutputStream fos;
|
||||
String fext = fn.substring(fn.lastIndexOf('.') + 1);
|
||||
byte[] buffer = new byte[1024];
|
||||
int count;
|
||||
if (!fext.equals("zip")) {
|
||||
fos = new FileOutputStream(path + ".zip");
|
||||
ZipOutputStream zipOut = new ZipOutputStream(fos);
|
||||
File fileToZip = new File(path);
|
||||
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
|
||||
zipOut.putNextEntry(zipEntry);
|
||||
while ((count = bis.read(buffer)) != -1) {
|
||||
zipOut.write(buffer, 0, count);
|
||||
}
|
||||
zipOut.close();
|
||||
bis.close();
|
||||
return;
|
||||
}
|
||||
fos = new FileOutputStream(path);
|
||||
while ((count = bis.read(buffer, 0, 1024)) != -1) {
|
||||
fos.write(buffer, 0, count);
|
||||
}
|
||||
fos.close();
|
||||
bis.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static void zipFile(String sourceFile) throws IOException {
|
||||
FileOutputStream fos = new FileOutputStream(sourceFile + ".zip");
|
||||
ZipOutputStream zipOut = new ZipOutputStream(fos);
|
||||
|
||||
File fileToZip = new File(sourceFile);
|
||||
FileInputStream fis = new FileInputStream(fileToZip);
|
||||
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
|
||||
zipOut.putNextEntry(zipEntry);
|
||||
byte[] bytes = new byte[1024];
|
||||
int length;
|
||||
while ((length = fis.read(bytes)) >= 0) {
|
||||
zipOut.write(bytes, 0, length);
|
||||
}
|
||||
zipOut.close();
|
||||
fis.close();
|
||||
fos.close();
|
||||
}
|
||||
|
||||
private static void downloadFileSet(String fn, int start, int end, String dirName) throws IOException {
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fn)))) {
|
||||
File dir = new File(dirName);
|
||||
dir.mkdirs();
|
||||
String line;
|
||||
int i = 0;
|
||||
while ((line = br.readLine()) != null) {
|
||||
if (i >= start) {
|
||||
if (i >= end) {
|
||||
break;
|
||||
}
|
||||
// LOGGER.debug("Start i={}, addr={}", i, line);
|
||||
try {
|
||||
downloadFile(line, dirName);
|
||||
} catch (IOException e) {
|
||||
LOGGER.debug("Downloading error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Funkcja wykorzystana jednokrotnie w celu integracji w jednym pliku wszystkich linków
|
||||
// do zasobów rozproszonych w wielu plikach.
|
||||
private static void readFileLinksFromGUGiKxml(String fn, HashMap<String, String> map) throws Exception {
|
||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||
FileInputStream is = new FileInputStream(fn);
|
||||
BufferedInputStream bis = new BufferedInputStream(is);
|
||||
// XMLStreamReader reader = factory.createXMLStreamReader(ClassLoader.getSystemResourceAsStream(fn));
|
||||
XMLStreamReader reader = factory.createXMLStreamReader(bis);
|
||||
String tagContent;
|
||||
boolean foundLink = false;
|
||||
LOGGER.debug("Poczatek odczytu z pliku: " + fn);
|
||||
while (reader.hasNext()) {
|
||||
int eventType = reader.next();
|
||||
switch (eventType) {
|
||||
case XMLStreamConstants.START_ELEMENT:
|
||||
String str = reader.getLocalName();
|
||||
if ("url_do_pobrania".equals(str)) {
|
||||
foundLink = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case XMLStreamConstants.CHARACTERS:
|
||||
if (foundLink) {
|
||||
tagContent = reader.getText().trim();
|
||||
String godlo = tagContent.substring(tagContent.lastIndexOf('_') + 1);
|
||||
godlo = godlo.substring(0, godlo.indexOf('.'));
|
||||
String old = map.remove(godlo);
|
||||
map.put(godlo, tagContent);
|
||||
foundLink = false;
|
||||
}
|
||||
break;
|
||||
|
||||
case XMLStreamConstants.END_ELEMENT:
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
reader.close();
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
58
src/main/java/pl/wat/ms4ds/terenfunkcje/nmt/NMTReader.java
Normal file
58
src/main/java/pl/wat/ms4ds/terenfunkcje/nmt/NMTReader.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package pl.wat.ms4ds.terenfunkcje.nmt;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class NMTReader {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
// File dir = new File(System.getProperty("user.home") + "/nmt/gugik_SkorowidzNMT2018.gml");
|
||||
|
||||
InputStream is = null;
|
||||
try {
|
||||
File file = new File("C:/Workspace/nmt/N-34-139-A-b-2-4.asc");
|
||||
is = new FileInputStream(file);
|
||||
readFromInputStream(is);
|
||||
//...
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if (is != null) {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void readFromInputStream(InputStream inputStream) throws IOException {
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||
String line= br.readLine();
|
||||
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 = Double.parseDouble(split[1]);
|
||||
line= br.readLine();
|
||||
split = line.split(" ");
|
||||
double yll = 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]);
|
||||
while ((line = br.readLine()) != null) {
|
||||
split = line.split(" ");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user