| 1 | package com.reallifedeveloper.tools.gis; | |
| 2 | ||
| 3 | import java.io.FileNotFoundException; | |
| 4 | import java.io.IOException; | |
| 5 | import java.net.URL; | |
| 6 | import java.nio.charset.Charset; | |
| 7 | import java.nio.charset.StandardCharsets; | |
| 8 | import java.util.HashMap; | |
| 9 | import java.util.Map; | |
| 10 | ||
| 11 | import org.geotools.api.data.DataStore; | |
| 12 | import org.geotools.api.data.DataStoreFinder; | |
| 13 | import org.geotools.api.data.SimpleFeatureSource; | |
| 14 | import org.geotools.api.feature.Feature; | |
| 15 | import org.geotools.data.simple.SimpleFeatureCollection; | |
| 16 | import org.geotools.data.simple.SimpleFeatureIterator; | |
| 17 | ||
| 18 | /** | |
| 19 | * Reads a <a href="http://en.wikipedia.org/wiki/Shapefile">shapefile</a> and processes it in some way. | |
| 20 | * <p> | |
| 21 | * One example of use is to create a version that converts a shapefile into SQL insert statements. | |
| 22 | * | |
| 23 | * @author RealLifeDeveloper | |
| 24 | */ | |
| 25 | public final class ShapefileProcessor { | |
| 26 | ||
| 27 | /** | |
| 28 | * The character encoding that is used by default, UTF-8. | |
| 29 | */ | |
| 30 | public static final Charset DEFAULT_CHARACTER_ENCODING = StandardCharsets.UTF_8; | |
| 31 | ||
| 32 | private final FeatureProcessor featureProcessor; | |
| 33 | private Charset characterEncoding = DEFAULT_CHARACTER_ENCODING; | |
| 34 | ||
| 35 | /** | |
| 36 | * Creates a new {@code ShapefileProcessor} that processes features in a shapefile using the given {@link FeatureProcessor}. | |
| 37 | * | |
| 38 | * @param featureProcessor the {@code FeatureProcessor} to use, must not be {@code null} | |
| 39 | */ | |
| 40 | public ShapefileProcessor(FeatureProcessor featureProcessor) { | |
| 41 |
1
1. <init> : negated conditional → KILLED |
if (featureProcessor == null) { |
| 42 | throw new IllegalArgumentException("featureProcessor must not be null"); | |
| 43 | } | |
| 44 | this.featureProcessor = featureProcessor; | |
| 45 | } | |
| 46 | ||
| 47 | /** | |
| 48 | * Processes a shapefile read from the given URL with the given {@link FeatureProcessor} that determines what to do with each individual | |
| 49 | * {@code org.opengis.feature.Feature} in the file. | |
| 50 | * | |
| 51 | * @param shapefileUrl a URL to the shapefile | |
| 52 | * | |
| 53 | * @throws IOException if there was a problem reading the shapefile | |
| 54 | */ | |
| 55 | public void processShapefile(URL shapefileUrl) throws IOException { | |
| 56 |
1
1. processShapefile : negated conditional → KILLED |
if (shapefileUrl == null) { |
| 57 | throw new IllegalArgumentException("shapefileUrl must not be null"); | |
| 58 | } | |
| 59 | Map<String, Object> connectParams = new HashMap<>(); | |
| 60 | connectParams.put("url", shapefileUrl); | |
| 61 | connectParams.put("charset", characterEncoding); | |
| 62 | ||
| 63 | DataStore dataStore = DataStoreFinder.getDataStore(connectParams); | |
| 64 |
1
1. processShapefile : negated conditional → KILLED |
if (dataStore == null) { |
| 65 | throw new FileNotFoundException(shapefileUrl.toString()); | |
| 66 | } | |
| 67 | String[] typeNames = dataStore.getTypeNames(); | |
| 68 | String typeName = typeNames[0]; | |
| 69 | ||
| 70 | SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName); | |
| 71 | SimpleFeatureCollection collection = featureSource.getFeatures(); | |
| 72 | ||
| 73 | try (SimpleFeatureIterator iterator = collection.features()) { | |
| 74 |
1
1. processShapefile : negated conditional → KILLED |
while (iterator.hasNext()) { |
| 75 |
1
1. processShapefile : removed call to com/reallifedeveloper/tools/gis/ShapefileProcessor$FeatureProcessor::processFeature → KILLED |
featureProcessor.processFeature(iterator.next()); |
| 76 | } | |
| 77 | } | |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * Sets the character encoding to use in the shapefile that is created. | |
| 82 | * | |
| 83 | * @param newCharacterEncoding the new character encoding | |
| 84 | */ | |
| 85 | public void setCharacterEncoding(Charset newCharacterEncoding) { | |
| 86 | this.characterEncoding = newCharacterEncoding; | |
| 87 | } | |
| 88 | ||
| 89 | /** | |
| 90 | * Defines some kind of processing of an {@code org.opengis.feature.Feature}. A {@code Feature} represents a composite object in a | |
| 91 | * shapefile that contains both a geographical or geometrical object and also other attributes. | |
| 92 | */ | |
| 93 | @FunctionalInterface | |
| 94 | public interface FeatureProcessor { | |
| 95 | /** | |
| 96 | * Processes the given {@code org.opengis.feature.Feature} in some way. | |
| 97 | * | |
| 98 | * @param feature the {@code Feature} to process | |
| 99 | */ | |
| 100 | void processFeature(Feature feature); | |
| 101 | } | |
| 102 | } | |
Mutations | ||
| 41 |
1.1 |
|
| 56 |
1.1 |
|
| 64 |
1.1 |
|
| 74 |
1.1 |
|
| 75 |
1.1 |