Inquire
Create shp
Data preparation
let param={<!-- -->code: "utf-8", datalist: [ {<!-- -->name:"place",type:0,height:2.5,geom:"POINT(109.588 34.645)"}, {<!-- -->name:"Incitement of Rebellion",type:2,heigh:3.2,geom:"POINT(109.588 34.645)"} ], filepath: "D:gisdata/gp/aaa/sx_test.shp", geomfiled: "geom", keylist: [ {<!-- --> filedname: "name", type: "string" },{<!-- --> filedname: "type", type: "int" },{<!-- --> filedname: "heigh", type: "double" } ], type: "Point" }
shp file creation and adding data
/** * Generate shape files * * @param shpPath Generate shape file path (including file name) filepath * @param encode encoding code * @param geoType map type, Point and Rolygon * @param shpKey data key geomfiled of the image frame * @param attrKeys attribute key collection keylist * @param data image frame and attribute collection datalist */ public void write2Shape(String shpPath, String encode, String geoType, String shpKey, List<ShpFiled> attrKeys, List<Map<String, Object>> data) {<!-- --> WKTReader reader = new WKTReader(); try {<!-- --> //Create shapefile object File file = new File(shpPath); Map<String, Serializable> params = new HashMap<>(); params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL()); ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params); //Define graphic information and attribute information SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.setCRS(DefaultGeographicCRS.WGS84); tb.setName("sx_test"); tb.add("the_geom", getClass(geoType)); for (ShpFiled field : attrKeys) {<!-- --> tb.add(field.getFiledname().toUpperCase(), getClass(field.getType())); } ds.createSchema(tb.buildFeatureType()); //Set encoding Charset charset = Charset.forName(encode); ds.setCharset(charset); //Set Writer FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT); //Write file information for (int i = 0; i < data.size(); i + + ) {<!-- --> SimpleFeature feature = writer.next(); Map<String, Object> row = data.get(i); Geometry geom = reader.read(row.get(shpKey).toString()); feature.setAttribute("the_geom", geom); for (ShpFiled field : attrKeys) {<!-- --> if (row.get(field.getFiledname()) != null) {<!-- --> feature.setAttribute(field.getFiledname().toUpperCase(), row.get(field.getFiledname())); } else {<!-- --> feature.setAttribute(field.getFiledname().toUpperCase(), null); } } } writer.write(); writer.close(); ds.dispose(); //Add to compressed file //zipShapeFile(shpPath); } catch (IOException e) {<!-- --> e.printStackTrace(); }catch (Exception e) {<!-- --> e.printStackTrace(); } }
shp new data
Data preparation
{<!-- -->filename:"D:\gisdata\gp\sx_qxzd.shp",list:[{<!-- -->'name':' dfs','geom': 'POINT(108.555 34.645)'}]}
Start adding
/** * Add data to shp file * * @param path file path * @param datalist space and attribute data */ public static void addFeature(String path, List<Map<String, Object>> datalist,String code) {<!-- --> ShapefileDataStore dataStore = null; File file = new File(path); try {<!-- --> dataStore = new ShapefileDataStore(file.toURL()); Charset charset = Charset.forName(code); dataStore.setCharset(charset); String typeName = dataStore.getTypeNames()[0]; SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource(typeName); SimpleFeatureType featureType = store.getSchema(); // Get the field list List<String> fileds = getFileds(featureType); // System.out.println(fileds.toString()); // Create Features List<SimpleFeature> list = new ArrayList<SimpleFeature>(); WKTReader reader = new WKTReader(); for (Map<String, Object> data : datalist) {<!-- --> SimpleFeatureBuilder build = new SimpleFeatureBuilder(featureType); if (data.get("geom") != null) {<!-- --> Geometry geometry = reader.read(data.get("geom").toString()); build.add(geometry); }else {<!-- --> build.add(null); } for (String filed : fileds) {<!-- --> Object object = data.get(filed); build.add(object); } SimpleFeature feature = build.buildFeature(null); list.add(feature); } SimpleFeatureCollection collection = new ListFeatureCollection(featureType, list); Transaction transaction = new DefaultTransaction("Adding"); store.setTransaction(transaction); try {<!-- --> store.addFeatures(collection); transaction.commit(); System.out.println("============addFeature=====done====="); } catch (Exception eek) {<!-- --> eek.printStackTrace(); transaction.rollback(); } } catch (MalformedURLException e) {<!-- --> // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) {<!-- --> // TODO Auto-generated catch block e.printStackTrace(); } catch (ParseException e) {<!-- --> // TODO Auto-generated catch block e.printStackTrace(); } }
shp update data
Data preparation
{<!-- -->filename required: "D:\gisdata\gp\sx_qxzd.shp",list:[{<!-- --> 'name':'dfs','geom': 'POINT(108.555 34.645)'},where:'FID_1=425']}
Start update
/** * Update shp file data * * @param path file path * @param datalist space and attribute data */ public static void updateFeature(String path, List<Map<String, Object>> datalist,String code) {<!-- --> ShapefileDataStore dataStore = null; File file = new File(path); Transaction transaction = new DefaultTransaction("handle"); try {<!-- --> dataStore = new ShapefileDataStore(file.toURL()); Charset charset = Charset.forName(code); dataStore.setCharset(charset); String typeName = dataStore.getTypeNames()[0]; SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource(typeName); // Get the field list SimpleFeatureType featureType = store.getSchema(); List<String> fileds = getFileds(featureType); store.setTransaction(transaction); WKTReader reader = new WKTReader(); for (Map<String, Object> data : datalist) {<!-- --> \t\t\t\t Filter filter = null; if (data.get("where") != null) {<!-- --> filter = ECQL.toFilter(data.get("where").toString()); } Object[] objs = new Object[] {<!-- -->}; String[] str = new String[] {<!-- -->}; if (data.get("geom") != null) {<!-- --> Geometry geometry = reader.read(data.get("geom").toString()); str = add(str, "the_geom"); objs = add(objs, geometry); } for (String stri : fileds) {<!-- --> if (data.get(stri) != null) {<!-- --> str = add(str, stri); objs = add(objs, data.get(stri)); } } store.modifyFeatures(str, objs, filter); } transaction.commit(); System.out.println("========updateFeature====end===="); } catch (Exception eek) {<!-- --> eek.printStackTrace(); try {<!-- --> transaction.rollback(); } catch (IOException e) {<!-- --> // TODO Auto-generated catch block e.printStackTrace(); } } }
shp remove data
Data preparation
{<!-- -->filename:"D:\gisdata\gp\sx_qxzd.shp",ids:[2,5,6],filed:"id"}
Start removal
/** * Remove data in shp * @param path file path * @param ids field value array * @param filed field name */ public static void removeFeature(String path, List<String>ids,String filed,String code){<!-- --> ShapefileDataStore dataStore = null; File file = new File(path); Transaction transaction = new DefaultTransaction("handle"); try {<!-- --> dataStore = new ShapefileDataStore(file.toURL()); Charset charset = Charset.forName(code); dataStore.setCharset(charset); String typeName = dataStore.getTypeNames()[0]; SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource(typeName); store.setTransaction(transaction); \t\t\t Filter filter = null; if(ids.size()>0) {<!-- --> String join = filed + " in (" + StringUtils.join(ids,",") + ")"; System.out.println(join); filter = ECQL.toFilter(join); } if(filter!=null) {<!-- --> \t\t \t store.removeFeatures(filter); transaction.commit(); System.out.println("======removeFeature== done ========"); } \t\t } catch (Exception eek) {<!-- --> eek.printStackTrace(); try {<!-- --> transaction.rollback(); } catch (IOException e) {<!-- --> // TODO Auto-generated catch block e.printStackTrace(); } \t } }