• 通过geotools读写shp文件


    依赖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();  
    	    }  
    	} 
    

      

  • 相关阅读:
    Pydev Console中文提示乱码的问题
    pg_dump命令帮助信息
    Aptana下Django1.6以后的项目模板结构改造
    老外的项目开发在线服务网站资源
    淘宝技术交流笔记
    Python并发编程的几篇文章
    gevent程序员指南
    IOS UITableView拖动排序功能
    Codeforces 300A Array
    一次mysql优化经历
  • 原文地址:https://www.cnblogs.com/lilei2blog/p/7792227.html
Copyright © 2020-2023  润新知