ShapeFileReader extends RecordReader<ShapeKey, ShpRecord> { public void close() throws IOException { shpInputStream.close(); } ShapeFileReader();  ShapeFileReader(int[] indexes); void initialize(InputSplit split, TaskAttemptContext context); boolean nextKeyValue(); ShapeKey getCurrentKey(); ShpRecord getCurrentValue(); float getProgress(); void close();  }
@Test public void testReadToPolygonRDD() throws Exception { String inputLocation = getShapeFilePath("polygon"); FeatureCollection<SimpleFeatureType, SimpleFeature> collection = loadFeatures(inputLocation); FeatureIterator<SimpleFeature> features = collection.features(); ArrayList<String> featureTexts = new ArrayList<String>(); while (features.hasNext()) { SimpleFeature feature = features.next(); Object geometry = feature.getDefaultGeometry(); if (geometry instanceof MultiPolygon) { MultiPolygon multiPolygon = (MultiPolygon) geometry; if (multiPolygon.getNumGeometries() == 1) { geometry = multiPolygon.getGeometryN(0); } } featureTexts.add(String.valueOf(geometry)); } features.close(); final Iterator<String> featureIterator = featureTexts.iterator(); PolygonRDD spatialRDD = ShapefileReader.readToPolygonRDD(sc, inputLocation); SpatialRDD<Geometry> geomeryRDD = ShapefileReader.readToGeometryRDD(sc, inputLocation); long count = RangeQuery.SpatialRangeQuery(spatialRDD, new Envelope(-180, 180, -90, 90), false, false).count(); assertEquals(spatialRDD.rawSpatialRDD.count(), count); for (Geometry geometry : geomeryRDD.rawSpatialRDD.collect()) { assertEquals(featureIterator.next(), geometry.toText()); } }
@Test public void testReadToLineStringRDD() throws Exception { String inputLocation = getShapeFilePath("polyline"); FeatureCollection<SimpleFeatureType, SimpleFeature> collection = loadFeatures(inputLocation); FeatureIterator<SimpleFeature> features = collection.features(); ArrayList<String> featureTexts = new ArrayList<String>(); while (features.hasNext()) { SimpleFeature feature = features.next(); featureTexts.add(String.valueOf(feature.getDefaultGeometry())); } features.close(); final Iterator<String> featureIterator = featureTexts.iterator(); LineStringRDD spatialRDD = ShapefileReader.readToLineStringRDD(sc, inputLocation); SpatialRDD<Geometry> geomeryRDD = ShapefileReader.readToGeometryRDD(sc, inputLocation); long count = RangeQuery.SpatialRangeQuery(spatialRDD, new Envelope(-180, 180, -90, 90), false, false).count(); assertEquals(spatialRDD.rawSpatialRDD.count(), count); for (Geometry geometry : geomeryRDD.rawSpatialRDD.collect()) { assertEquals(featureIterator.next(), geometry.toText()); } }
@Test public void testReadToPointRDD_Point() throws Exception { String inputLocation = getShapeFilePath("point"); FeatureCollection<SimpleFeatureType, SimpleFeature> collection = loadFeatures(inputLocation); FeatureIterator<SimpleFeature> features = collection.features(); ArrayList<String> featureTexts = new ArrayList<String>(); while (features.hasNext()) { SimpleFeature feature = features.next(); featureTexts.add(String.valueOf(feature.getDefaultGeometry())); } features.close(); final Iterator<String> featureIterator = featureTexts.iterator(); PointRDD spatialRDD = ShapefileReader.readToPointRDD(sc, inputLocation); long count = RangeQuery.SpatialRangeQuery(spatialRDD, new Envelope(-180, 180, -90, 90), false, false).count(); assertEquals(spatialRDD.rawSpatialRDD.count(), count); for (Geometry geometry : spatialRDD.rawSpatialRDD.collect()) { assertEquals(featureIterator.next(), geometry.toText()); } }
@Test public void testReadToPointRDD_MultiPoint() throws IOException { String inputLocation = getShapeFilePath("multipoint"); FeatureCollection<SimpleFeatureType, SimpleFeature> collection = loadFeatures(inputLocation); FeatureIterator<SimpleFeature> features = collection.features(); ArrayList<String> featureTexts = new ArrayList<String>(); while (features.hasNext()) { SimpleFeature feature = features.next(); featureTexts.add(String.valueOf(feature.getDefaultGeometry())); } features.close(); final Iterator<String> featureIterator = featureTexts.iterator(); PointRDD spatialRDD = ShapefileReader.readToPointRDD(sc, inputLocation); SpatialRDD<Geometry> geomeryRDD = ShapefileReader.readToGeometryRDD(sc, inputLocation); for (Geometry geometry : geomeryRDD.rawSpatialRDD.collect()) { assertEquals(featureIterator.next(), geometry.toText()); } }
@Test public void testLoadDbfFile() throws IOException { String inputLocation = getShapeFilePath("dbf"); FeatureCollection<SimpleFeatureType, SimpleFeature> collection = loadFeatures(inputLocation); FeatureIterator<SimpleFeature> features = collection.features(); ArrayList<String> featureTexts = new ArrayList<String>(); while (features.hasNext()) { SimpleFeature feature = features.next(); String attr = ""; int i = 0; for (Property property : feature.getProperties()) { if (i == 0) { i++; continue; } if (i > 1) { attr += "\t"; } attr += String.valueOf(property.getValue()); i++; } featureTexts.add(attr); } features.close(); final Iterator<String> featureIterator = featureTexts.iterator(); for (Geometry geometry : ShapefileReader.readToGeometryRDD(sc, inputLocation).rawSpatialRDD.collect()) { assertEquals(featureIterator.next(), geometry.getUserData()); } }
@Test public void testReadBoundary() throws IOException { String inputLocation = getShapeFilePath("dbf"); ShpFiles shpFile = new ShpFiles(inputLocation + "/map.shp"); GeometryFactory geometryFactory = new GeometryFactory(); org.geotools.data.shapefile.shp.ShapefileReader gtlReader = new org.geotools.data.shapefile.shp.ShapefileReader(shpFile, false, true, geometryFactory); String gtlbounds = gtlReader.getHeader().minX() + ":" + gtlReader.getHeader().minY() + ":" + gtlReader.getHeader().maxX() + ":" + gtlReader.getHeader().maxY(); BoundBox bounds = ShapefileReader.readBoundBox(sc, inputLocation); String myBounds = bounds.getXMin() + ":" + bounds.getYMin() + ":" + bounds.getXMax() + ":" + bounds.getYMax(); assertEquals(gtlbounds, myBounds); gtlReader.close(); }