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