SHPParser implements FeatureCollectionParser { @Override public SimpleFeatureCollection parse(File file, CoordinateReferenceSystem sourceCRS, CoordinateReferenceSystem targetCRS) throws ServiceException { ShapefileDataStore store = null; try { store = new ShapefileDataStore(file.toURI().toURL()); store.setCharset(getCharset(file)); String typeName = store.getTypeNames()[0]; SimpleFeatureSource source = store.getFeatureSource(typeName); CoordinateReferenceSystem crs = source.getSchema() .getGeometryDescriptor() .getCoordinateReferenceSystem(); if (crs != null) { sourceCRS = crs; } return FeatureCollectionParsers.read(source, sourceCRS, targetCRS); } catch (ServiceException e) { throw e; } catch (Exception e) { throw new UserLayerException("Failed to parse SHP: " + e.getMessage(), UserLayerException.ErrorType.PARSER, UserLayerException.ErrorType.INVALID_FORMAT); } finally { if (store != null) { store.dispose(); } } }  @Override SimpleFeatureCollection parse(File file, CoordinateReferenceSystem sourceCRS,
            CoordinateReferenceSystem targetCRS); @Override String getSuffix(); static final String SUFFIX; }
@Test public void testParse() throws ServiceException, URISyntaxException, NoSuchAuthorityCodeException, FactoryException { SimpleFeature shp16 = getSHP16(); Geometry geom = (Geometry) shp16.getDefaultGeometry(); assertTrue(geom instanceof MultiPolygon); MultiPolygon mp = (MultiPolygon) geom; assertEquals(14, mp.getNumGeometries()); Polygon p = (Polygon) mp.getGeometryN(0); CoordinateSequence exterior = p.getExteriorRing().getCoordinateSequence(); Coordinate first = exterior.getCoordinate(0); assertEquals(194831.89, first.x, 1e6); assertEquals(6947066.665, first.y, 1e6); assertTrue(Double.isNaN(first.z)); }
@Test public void testParseToDifferentProjecion() throws ServiceException, URISyntaxException, NoSuchAuthorityCodeException, FactoryException { File file = new File(SHPParserTest.class.getResource("SHP2017.shp").toURI()); SHPParser parser = new SHPParser(); SimpleFeatureCollection fc = parser.parse(file, null, CRS.decode("EPSG:4326")); ReferencedEnvelope bounds = fc.getBounds(); CoordinateReferenceSystem crs = bounds.getCoordinateReferenceSystem(); assertEquals("Bounds is transformed correctly", "EPSG:4326", CRS.toSRS(crs)); assertEquals(59.80841, bounds.getMinX(), 1e-5); assertEquals(19.47275, bounds.getMinY(), 1e-5); assertEquals(70.09210, bounds.getMaxX(), 1e-5); assertEquals(31.58671, bounds.getMaxY(), 1e-5); }