|
1 | 1 | package com.opendoorlogistics.core.api.impl;
|
2 | 2 |
|
3 | 3 | import java.io.File;
|
| 4 | +import java.net.URL; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
4 | 9 |
|
5 | 10 | import org.apache.commons.io.FilenameUtils;
|
| 11 | +import org.geotools.data.DataStore; |
| 12 | +import org.geotools.data.DataStoreFinder; |
| 13 | +import org.opengis.feature.simple.SimpleFeatureType; |
6 | 14 |
|
7 | 15 | import com.opendoorlogistics.api.ExecutionReport;
|
8 | 16 | import com.opendoorlogistics.api.IO;
|
9 |
| -import com.opendoorlogistics.api.ODLApi; |
10 | 17 | import com.opendoorlogistics.api.components.ProcessingApi;
|
11 | 18 | import com.opendoorlogistics.api.io.ImportFileType;
|
12 |
| -import com.opendoorlogistics.api.scripts.ScriptOption; |
13 | 19 | import com.opendoorlogistics.api.tables.ODLDatastore;
|
14 | 20 | import com.opendoorlogistics.api.tables.ODLDatastoreAlterable;
|
| 21 | +import com.opendoorlogistics.api.tables.ODLTable; |
15 | 22 | import com.opendoorlogistics.api.tables.ODLTableAlterable;
|
16 | 23 | import com.opendoorlogistics.api.tables.ODLTableReadOnly;
|
17 | 24 | import com.opendoorlogistics.core.AppConstants;
|
18 |
| -import com.opendoorlogistics.core.api.impl.scripts.ScriptOptionImpl; |
19 |
| -import com.opendoorlogistics.core.components.ODLGlobalComponents; |
20 |
| -import com.opendoorlogistics.core.scripts.elements.Script; |
21 |
| -import com.opendoorlogistics.core.scripts.io.ScriptIO; |
| 25 | +import com.opendoorlogistics.core.geometry.ImportShapefile; |
| 26 | +import com.opendoorlogistics.core.geometry.rog.RogReaderUtils; |
22 | 27 | import com.opendoorlogistics.core.tables.io.PoiIO;
|
23 | 28 | import com.opendoorlogistics.core.tables.io.TableIOUtils;
|
| 29 | +import com.opendoorlogistics.core.utils.Exceptions; |
| 30 | +import com.opendoorlogistics.core.utils.io.RelativeFiles; |
24 | 31 | import com.opendoorlogistics.core.utils.strings.Strings;
|
25 | 32 |
|
26 | 33 | public class IOImpl implements IO{
|
@@ -80,4 +87,55 @@ public File getLoadedExcelFile() {
|
80 | 87 | return null;
|
81 | 88 | }
|
82 | 89 |
|
| 90 | + @Override |
| 91 | + public List<String> getShapefileFieldnames(File shapefileOrODLRG) { |
| 92 | + // turn an .odlrg file reference into a shapefile reference |
| 93 | + if(Strings.equalsStd(FilenameUtils.getExtension(shapefileOrODLRG.getName()), RogReaderUtils.RENDER_GEOMETRY_FILE_EXT)){ |
| 94 | + String s = FilenameUtils.removeExtension(shapefileOrODLRG.getAbsolutePath()); |
| 95 | + s += ".shp"; |
| 96 | + shapefileOrODLRG = new File(s); |
| 97 | + } |
| 98 | + |
| 99 | + DataStore shapefile = null; |
| 100 | + try { |
| 101 | + Map<String, URL> map = new HashMap<String, URL>(); |
| 102 | + map.put("url", shapefileOrODLRG.toURI().toURL()); |
| 103 | + shapefile = DataStoreFinder.getDataStore(map); |
| 104 | + |
| 105 | + // check not corrupt |
| 106 | + if (shapefile.getTypeNames().length != 1) { |
| 107 | + throw new RuntimeException("Shapefile should only contain one type"); |
| 108 | + } |
| 109 | + |
| 110 | + String typename = shapefile.getTypeNames()[0]; |
| 111 | + SimpleFeatureType schema = shapefile.getSchema(typename); |
| 112 | + int nAttrib = schema.getAttributeCount(); |
| 113 | + ArrayList<String> ret = new ArrayList<>(); |
| 114 | + for (int i = 0; i < nAttrib; i++) { |
| 115 | + ret.add(schema.getDescriptor(i).getLocalName()); |
| 116 | + } |
| 117 | + return ret; |
| 118 | + |
| 119 | + } catch (Throwable e) { |
| 120 | + throw Exceptions.asUnchecked(e); |
| 121 | + } finally { |
| 122 | + |
| 123 | + if (shapefile != null) { |
| 124 | + shapefile.dispose(); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public ODLTable importShapefile(File file, int maxRows) { |
| 131 | + ODLDatastoreAlterable<? extends ODLTableAlterable> ds=new ODLApiImpl().tables().createAlterableDs(); |
| 132 | + ImportShapefile.importShapefile(file, false, ds, false, maxRows); |
| 133 | + return ds.getTableAt(0); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public File getAsRelativeIfWithinStandardShapefileDirectory(File file) { |
| 138 | + return RelativeFiles.makeRelativeIfAbsoluteWithinDefaultDirectory(file.getAbsolutePath(), AppConstants.SHAPEFILES_DIRECTORY); |
| 139 | + } |
| 140 | + |
83 | 141 | }
|
0 commit comments