The SHP format is a vector data format used to store geographic information system (GIS) data.
SHP files consist of a series of ordered files. The shp files we export include .shp, .shx, .dbf, .prj and .fix files.
- .shp (shape) file: stores vector map data and records the spatial location information of each feature.
- .shx (shape index) file: It is an index file, used to store the location of elements in the .shp file to speed up data access.
- .dbf (dBase) file: stores attribute information of vector data, such as the name, type and other information of each point on the map.
- .prj (projection) file: It is a map coordinate system file, which contains map projection information.
- .fix file: fid index file
The code for exporting shp files is implemented as follows:
<properties> <geotools-version>28.2</geotools-version> </properties> <dependencies> <!-- geotools--> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-main</artifactId> <version>${geotools-version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-shapefile</artifactId> <version>${geotools-version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-metadata</artifactId> <version>${geotools-version}</version> </dependency> </dependencies> <!-- geotools warehouse--> <repositories> <repository> <id>osgeo</id> <name>Open Source Geospatial Foundation Repository</name> <url>https://repo.osgeo.org/repository/release/</url> </repository> <repository> <id>osgeo-snapshot</id> <name>OSGeo Snapshot Repository</name> <url>https://repo.osgeo.org/repository/snapshot/</url> </repository> </repositories> </project>
public class ExportShp {<!-- --> /** * Export shp file * * @param dataPropertiesList property list {property name: property value} * @param fileName export shp file name * @param geomType geometry type */ private static void exportShp(List<Map<String, Object>> dataPropertiesList, String fileName, String geomType) {<!-- --> //Create and save shp folder String saveFolder = "D:/workspace/vector/vector/exportShp/"; File dir = new File(saveFolder); if (!dir.exists()) {<!-- --> FileUtil.mkdir(dir); } //shp file path String shpFileName = fileName + ".shp"; String fileUrl = saveFolder + shpFileName; File file = new File(fileUrl); FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null; ShapefileDataStore ds = null; try {<!-- --> Map<String, Serializable> params = new HashMap<>(); params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL()); ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params); //Define graphic information and attribute information SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); //Set the coordinate system CoordinateReferenceSystem crs84 = CRS.decode("EPSG:4326", true); tb.setCRS(crs84); //Set file name tb.setName(fileName); //Define the plot attribute name of the exported shp file String geomProperty = "the_geom"; String idProperty = "ID"; String nameProperty = "name"; String descriptionProperty = "desc"; //Set graphics type if ("Polygon".equals(geomType)) {<!-- --> tb.add(geomProperty, Polygon.class); } else if ("MultiPolygon".equals(geomType)) {<!-- --> tb.add(geomProperty, MultiPolygon.class); } else if ("Point".equals(geomType)) {<!-- --> tb.add(geomProperty, Point.class); } else if ("MultiPoint".equals(geomType)) {<!-- --> tb.add(geomProperty, MultiPoint.class); } else if ("LineString".equals(geomType)) {<!-- --> tb.add(geomProperty, LineString.class); } else if ("MultiLineString".equals(geomType)) {<!-- --> tb.add(geomProperty, MultiLineString.class); } else {<!-- --> throw new BizIllegalArgumentException("There is no such type in Geometry: " + geomType); } //Set the corresponding attribute type tb.add(idProperty, String.class); tb.add(nameProperty, String.class); tb.add(descriptionProperty, String.class); //Set the default geometry tb.setDefaultGeometry(geomProperty); //create ds.createSchema(tb.buildFeatureType()); ds.setCharset(StandardCharsets.UTF_8); //Set Writer writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT); SimpleFeature feature; for (Map<String, Object> map : dataPropertiesList) {<!-- --> feature = writer.next(); //Attribute assignment geometry should be assigned in wkt format feature.setAttribute(geomProperty, new WKTReader().read((MapUtil.getStr(map, "geometry")))); feature.setAttribute(idProperty, MapUtil.getStr(map, idProperty)); feature.setAttribute(nameProperty, MapUtil.getStr(map, "name")); String description = MapUtil.getStr(map, "Description"); if (CharSequenceUtil.isNotBlank(description)) {<!-- --> feature.setAttribute(descriptionProperty, description); } } writer.write(); } catch (IOException | FactoryException | ParseException e) {<!-- --> e.printStackTrace(); } finally {<!-- --> //Close the relevant stream try {<!-- --> if (writer != null) {<!-- --> writer.close(); } } catch (IOException e) {<!-- --> e.printStackTrace(); } if (ds != null) {<!-- --> ds.dispose(); } } } public static void main(String[] args) {<!-- --> List<Map<String,Object>> propertyList = new ArrayList<>(); for (int i = 0; i < 3; i + + ) {<!-- --> Map<String,Object> map = new HashMap<>(); map.put("ID", i); map.put("name", "test" + i); map.put("Description", "Test shp export" + i); map.put("geometry", "MULTILINESTRING ((114.0888763800001 22.549298400000055, 114.0897166200001 22.54931240800005, 114.09006708000004 22.54931825 0000056, 114.09104754000009 22.549328150000065))"); propertyList.add(map); } exportShp(propertyList, "test", "MultiLineString"); } }
The exported file is as follows: