依赖jar包
读取shp
public static void main(String[] path) { DbaseFileReader reader = null; try { reader = new DbaseFileReader(new ShpFiles("C:\Users\lilei3774\Desktop\wuhan\1\split-hanzi\poi.shp"), false, Charset.forName("utf-8")); DbaseFileHeader header = reader.getHeader(); int numFields = header.getNumFields(); while (reader.hasNext()) { try { Object[] entry = reader.readEntry(); for (int i = 0; i < numFields; i++) { String title = header.getFieldName(i); Object value = entry[i]; System.out.println(title + "=" + value); } } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (Exception e) { } } } }
public static void main(String[] args) { // TODO Auto-generated method stub ShapefileDataStore shpDataStore = null; try{ shpDataStore = new ShapefileDataStore(new File("C:\Users\lilei3774\Desktop\wuhan\1\split-hanzi\poi.shx").toURI().toURL()); // shpDataStore.setStringCharset(Charset.forName("GBK")); shpDataStore.setCharset(Charset.forName("utf-8")); String typeName = shpDataStore.getTypeNames()[0]; FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null; featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>)shpDataStore.getFeatureSource(typeName); FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures(); System.out.println(result.size()); FeatureIterator<SimpleFeature> itertor = result.features(); while(itertor.hasNext()){ SimpleFeature feature = itertor.next(); Collection<Property> p = feature.getProperties(); Iterator<Property> it = p.iterator(); while(it.hasNext()) { Property pro = it.next(); if (pro.getValue() instanceof Point) { System.out.println("PointX = " + ((Point)(pro.getValue())).getX()); System.out.println("PointY = " + ((Point)(pro.getValue())).getY()); } else { System.out.println(pro.getName() + " = " + pro.getValue()); } } } itertor.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } }
写shp
public static void main(String[] args) { try{ File file = new File("C:\Users\lilei3774\Desktop\wuhan\1\split-hanzi\self.shp"); Map<String, Serializable> params = new HashMap<String, Serializable>(); params.put( ShapefileDataStoreFactory.URLP.key, file.toURI().toURL() ); ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params); //定义图形信息和属性信息 SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.setCRS(DefaultGeographicCRS.WGS84); tb.setName("shapefile"); tb.add("the_geom", Point.class); tb.add("POIID", Long.class); tb.add("NAMEC", String.class); ds.createSchema(tb.buildFeatureType()); ds.setCharset(Charset.forName("utf-8")); //设置Writer FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT); //写下一条 SimpleFeature feature = writer.next(); feature.setAttribute("the_geom", new GeometryFactory().createPoint(new Coordinate(116.123, 39.345))); feature.setAttribute("POIID", 1234567890l); feature.setAttribute("NAMEC", "某兴趣点1"); feature = writer.next(); feature.setAttribute("the_geom", new GeometryFactory().createPoint(new Coordinate(116.456, 39.678))); feature.setAttribute("POIID", 1234567891l); feature.setAttribute("NAMEC", "某兴趣点2"); writer.write(); writer.close(); ds.dispose(); } catch (Exception e) { e.printStackTrace(); } }