Hey StackOverflow社区, 我目前正在尝试编写一个小工具,它读取shapefile的几何图形(Multipolygons / Polygons),并将这些工具的WKT表示写入文本文件。 为了做到这一点,我正在使用GeoTools并且我设法让它运行正常,因为我正在转换大约5000000个多边形/多边形的文件,这需要很长时间才能完成。
所以我的问题是:
是否可以加载文件加载/写入? 当我使用SimpleFeatureIterator时,我没有找到如何实现多线程。
有没有办法这样做? 或者有人知道,如何在不使用迭代器的情况下获取shapefile几何图形?
这是我的代码:
此方法只是声明文件选择器并为每个选定文件启动线程。
protected static void printGeometriesToFile() {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"shape-files", "shp");
chooser.setFileFilter(filter);
chooser.setDialogTitle("Choose the file to be converted.");
chooser.setMultiSelectionEnabled(true);
File[] files = null;
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
files = chooser.getSelectedFiles();
}
for (int i = 0; i < files.length; i++) {
MultiThreadWriter writer = new MultiThreadWriter(files[i]);
writer.start();
}
}
多线程类:
class MultiThreadWriter extends Thread {
private File threadFile;
MultiThreadWriter(File file) {
threadFile = file;
System.out.println("Starting Thread for " + file.getName());
}
public void run() {
try {
File outputFolder = new File(threadFile.getAbsolutePath() + ".txt");
FileOutputStream fos = new FileOutputStream(outputFolder);
System.out.println("Now writing data to file: " + outputFolder.getName());
FileDataStore store = FileDataStoreFinder.getDataStore(threadFile);
SimpleFeatureSource featureSource = store.getFeatureSource();
SimpleFeatureCollection featureCollection = featureSource.getFeatures();
SimpleFeatureIterator featureIterator = featureCollection.features();
int pos = 0;
while (featureIterator.hasNext()) {
fos.write((geometryToByteArray((Polygonal) featureIterator.next().getAttribute("the_geom"))));
pos++;
System.out.println("The file " + threadFile.getName() + "'s current positon is: " + pos);
}
fos.close();
System.out.println("Finished writing.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
这只是一个帮助函数,它将多面体转换为多边形,并使用&#34; |&#34;返回其WKT表示。作为一个分离者。
private byte[] geometryToByteArray(Polygonal polygonal) {
List<Polygon> polygonList;
String polygonString = "";
if (polygonal instanceof MultiPolygon) {
polygonList = GeometrieUtils.convertMultiPolygonToPolygonList((MultiPolygon) polygonal);
//The method above just converts a MultiPolygon into a list of Polygons
} else {
polygonList = new ArrayList<>(1);
polygonList.add((Polygon) polygonal);
}
for (int i = 0; i < polygonList.size(); i++) {
polygonString = polygonString + polygonList.get(i).toString() + "|";
}
return polygonString.getBytes();
}
}
我知道我的代码不是很好或不好。我刚刚开始学习Java,希望它能很快好起来。
真诚
ihavenoclue:)
答案 0 :(得分:1)
您不需要为每个文件创建新线程,因为创建新线程是一项昂贵的操作。相反,您可以让MultiThreadWriter
实现Runnable
并使用ThreadPoolExecuter
管理所有主题。
MultiThreadWriter
public class MultiThreadWriter implements Runnable {
@Override
public void run() {
//
}
}
创建线程池与运行时处理器匹配。
ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
for (int i = 0; i < files.length; i++) {
MultiThreadWriter writer = new MultiThreadWriter(files[i]);
service.submit(writer);
}
您可以使用BufferedWriter
代替OutputStream
,当您重复编写小件时,more
efficient。
File outputFolder = new File(threadFile.getAbsolutePath() + ".txt");
FileOutputStream fos = new FileOutputStream(outputFolder);
BufferedWriter writer = new BufferedWriter(fos);
答案 1 :(得分:0)
我更愿意将文件内容作为对象列表读取,然后将列表拆分为子列表,然后为每个列表创建一个线程,例如:
int nbrThreads = 10;
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(nbrThreads);
int count = myObjectsList != null ? myObjectsList.size() / nbrThreads : 0;
List<List<MyObject>> resultlists = choppeList(myObjectsList, count > 0 ? count : 1);
try
{
for (List<MyObject> list : resultlists)
{
// TODO : create your thread and passe the list of objects
}
executor.shutdown();
executor.awaitTermination(30, TimeUnit.MINUTESS); // chose time of termination
}
catch (Exception e)
{
LOG.error("Problem launching threads", e);
}
choppeList方法可以是这样的:
public <T> List<List<T>> choppeList(final List<T> list, final int L)
{
final List<List<T>> parts = new ArrayList<List<T>>();
final int N = list.size();
for (int i = 0; i < N; i += L)
{
parts.add(new ArrayList<T>(list.subList(i, Math.min(N, i + L))));
}
return parts;
}