最近被借到别的项目组写地图矢量信息产品导出。点线面数值的存储。

在网上一直没有找到比较全的例子,有借鉴一些网上的三角形要素方法下面有链接。

参考:

 

然后配置环境网上也没找到合适的,找同事写C的大神要了份环境。代码也是别的部门做过,有了一两个小案例。然后自己摸索存执,图形复合项目要求。在这里做如下总结。主要分为,一、环境搭建。二、点矢量数据编写。三、线矢量数据编写。四、面的矢量数据编写。就是参考案例。我会尽可能的详细的做注解方法理解。

一、环境搭建。

    网上找的很多,下载编译好的gdal的jar包,和四五个dll文件,主要是ogrjni.dll。但是这样会发现缺少dll组件,于是通过

java实现矢量面抽稀算法 java矢量组件_矢量文件

这个软件,一直寻找缺少的dll,很蠢。

    其实gdal,java只做了一点博博的封装,需要c的库才能调用(gdal201.dll),还需要GEOS和Proj4库的支持。下面贴出需要的所有dll文件和测试驱动链接代码。(在测试dll文件都是放到项目根目录下。整合项目的时候,需要把文件放到C盘windows下system32目录下。当然大家一定不能忘了还要gdal.jar文件。下图还有生成后的四个文件testPoint112.等四个后缀文件。不知道什么用可以百度。);

java实现矢量面抽稀算法 java矢量组件_java操作gdal_02

 

    测试驱动链接的代码:

public static void main(String[] args)   
 {  
 String fileName_tif = "D:\\band1.tif";    
 gdal.AllRegister();

 int nDriverCount = gdal.GetDriverCount();
 String driversInfo = String.format("GDAL Driver Count is:%s", nDriverCount);
 System.out.println(driversInfo);

 for(int i=0;i<nDriverCount;i++)
 {
 Driver driver = gdal.GetDriver(i);
 String strDriverName = driver.getShortName();
 System.out.println(strDriverName);
 }


 Dataset hDataset = gdal.Open(fileName_tif, gdalconstConstants.GA_ReadOnly);  
 if (hDataset == null)  
 {  
 System.err.println("GDALOpen failed - " + gdal.GetLastErrorNo());  
 System.err.println(gdal.GetLastErrorMsg());  


 System.exit(1);  
 }  


 Driver hDriver = hDataset.GetDriver();  
 System.out.println("Driver: " + hDriver.getShortName() + "/" + hDriver.getLongName());  


 System.out.println("Size is " + hDataset.getRasterXSize() + ", " + hDataset.getRasterYSize()); 


 hDataset.delete();  


 // ��ѡ  
 gdal.GDALDestroyDriverManager();


  }  
如果打印出一些英文驱动信息,就表示环境部署成功。
 
二、点矢量数据编写。
    话不多说直接上代码了。
/**
 * 生成单个点
 */
 @Test
 public void test01(){
 String strVectorFile = "D:\\TJFXshp\\Point001.shp";
 // 注册所有的驱动
 ogr.RegisterAll();
 // 为了支持中文路径,请添加下面这句代码
 gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
 // 为了使属性表字段支持中文,请添加下面这句
 gdal.SetConfigOption("SHAPE_ENCODING", "");
 // 创建数据,这里以创建ESRI的shp文件为例
 String strDriverName = "ESRI Shapefile";
 // 调用对shp文件读写的driver
 org.gdal.ogr.Driver oDriver =ogr.GetDriverByName(strDriverName);  
 if (oDriver == null){  
 System.out.println(strVectorFile+ " 驱动不可用!\n");  
         return;  
     } 
 // 创建数据源  
         DataSource oDS = oDriver.CreateDataSource(strVectorFile,null);  
         if (oDS == null){  
         System.out.println("创建矢量文件【"+ strVectorFile +"】失败!\n" );  
         return;  
         }  
         // 创建图层layer  参数说明:新图层名称,坐标系,图层的几何类型,创建选项,与驱动有关
         SpatialReference ref = new SpatialReference();// 空间参数
 ref.SetWellKnownGeogCS("WGS84");
         Layer pLayer =oDS.CreateLayer("TestPoint", ref, ogr.wkbPoint, null);  
         if (pLayer == null){  
              System.out.println("图层创建失败!\n");  
              return;  
         } 
 // 创建属性
         FieldDefn oFieldID = new FieldDefn("ID", ogr.OFTInteger); 
         pLayer.CreateField(oFieldID, 1); 
         oFieldID.SetWidth(5);  
         // 再创建一个"X"属性
         FieldDefn oFieldlon = new FieldDefn("lon", ogr.OFTString);  
         pLayer.CreateField(oFieldlon, 2); 
         oFieldlon.SetWidth(30);  
         // 再创建一个"Y"属性
         FieldDefn oFieldlat = new FieldDefn("lat", ogr.OFTString);  
         pLayer.CreateField(oFieldlat, 3); 
         oFieldlat.SetWidth(30); 
         // 创建一个"value"属性
         FieldDefn oFieldVal = new FieldDefn("Val", ogr.OFTString);  
         pLayer.CreateField(oFieldVal, 4); 
         oFieldVal.SetWidth(30); 
         // GetLayerDefn()获取当前图层的属性表结构
         FeatureDefn oDefn =pLayer.GetLayerDefn();  
         // 创建一个feature
         Feature poFeature  = new Feature(oDefn);  
         int i = 0;
         poFeature.SetField("ID", i);  
         poFeature.SetField("lon", 12.123);  
         poFeature.SetField("lat", 123.12);  
         poFeature.SetField("Val", 27);  
         i++;
         // 创建一个point对象
         Geometry lineG = new Geometry(ogr.wkbPoint);
 lineG.AddPoint(12.123, 123.12);
         poFeature.SetGeometry(lineG);
         // 生成Feature
         pLayer.CreateFeature(poFeature);
         
         pLayer.SyncToDisk();  
         oDS.SyncToDisk();  
   
         System.out.println("\n数据集创建完成!\n");          
 }

    

三、线矢量数据编写

@Test
 public void test04() throws Exception {
 // 到项目中应该是两个数组
 // 比较数组数量一样,然后遍历,每次put进入一个map,后者在put前调用数据处理方法
 Map map = new HashMap<Double, String>();
 // 拼接line的经纬度
 String parameter1 = "53.3750,46.6833,53.2269,46.6250,53.3750,46.4830,53.6250,46.5667,53.7513,46.3750,53.8750,46.2817,54.1250,46.3289,54.3750,46.3604,54.3916,46.3750,54.3755,46.3918";
 String parameter2 = "53.3295,47.9205,53.2795,47.8750,53.2422,47.6250,53.3046,47.3750,53.3750,47.3609,53.5886,47.1250,53.4145,46.8750,53.3750,46.8695,53.3184,46.8750,53.1250,46.8938";
 String parameter3 = "53.7956,47.7044,53.6250,47.6865,53.4357,47.8750";
 String parameter4 = "12.1212,54.6212,12.1250,54.5433,12.2119,54.6250";
 String str1 = stringvalueListToWKTLineString(parameter1);
 String str2 = stringvalueListToWKTLineString(parameter2);
 String str3 = stringvalueListToWKTLineString(parameter3);
 String str4 = stringvalueListToWKTLineString(parameter4);
 System.out.println(str1);
 System.out.println(str2);
 System.out.println(str3);
 System.out.println(str4);
 map.put(29.00, str1);
 map.put(30.00, str2);
 map.put(31.00, str3);
 map.put(32.00, str4);
 String filePath = "D:\\TJFXshp\\Line002.shp";
 newCreateLineShapeFromCoordList(map,filePath);}
 
/**
 * 
 * @param map
 *            key:一条线的值 value:集合String 生成线的坐标
 * @param filepath
 *            文件路径
 */
 public static void newCreateLineShapeFromCoordList(Map<Double, String> map,
 String filepath) {
 File file = new File(filepath);
 File parentDirectory = file.getParentFile();
 if (!parentDirectory.exists()) {
 parentDirectory.mkdirs();
 }
 deleteShpFile(file);


 ogr.RegisterAll();
 gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
 gdal.SetConfigOption("SHAPE_ENCODING", "");


 String strDriverName = "ESRI Shapefile";
 org.gdal.ogr.Driver oDriver = ogr.GetDriverByName(strDriverName);
 if (oDriver == null) {
 return;
 }


 DataSource oDS = oDriver.CreateDataSource(filepath, null);
 SpatialReference ref = new SpatialReference();
 ref.SetWellKnownGeogCS("WGS84");
 Layer oLayer = oDS.CreateLayer("contourLine", ref, ogr.wkbLineString,
 null);
 if (oLayer == null) {
 return;
 }
 // 创建一个"value"属性
 FieldDefn oFieldV = new FieldDefn("value", ogr.OFTString);
 oLayer.CreateField(oFieldV, 1);
 oFieldV.SetWidth(30);
 FeatureDefn oFeatureDefn = oLayer.GetLayerDefn();


 Set<Entry<Double, String>> entrySet = map.entrySet();
 for (Entry<Double, String> entry : entrySet) {
 if (entry.getValue() != null && !"".equals(entry.getValue())) {
 String geometryVal = entry.getValue();
 // 每一个String创建一个要素
 Feature pFeature = new Feature(oFeatureDefn);
 if (entry.getKey() != null && !"".equals(entry.getKey())) {
 // 获取value值 并赋值
 double value = entry.getKey();
 pFeature.SetField("value", value);
 // 创建line线形图
 Geometry geomRectangle = Geometry.CreateFromWkt(geometryVal);
 pFeature.SetGeometry(geomRectangle);
 oLayer.CreateFeature(pFeature);
 }
 }
 }


 oLayer.delete();
 oDS.delete();}
 
四、面的矢量数据编写
@Test
 public void test01(){


         String strVectorFile ="D:\\TJFXshp\\PlaneShap001.shp";    
     
         // 注册所有的驱动  
         ogr.RegisterAll();  
          
         // 为了支持中文路径,请添加下面这句代码  
         gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","NO");  
         // 为了使属性表字段支持中文,请添加下面这句  
         gdal.SetConfigOption("SHAPE_ENCODING","");  
   
         //创建数据,这里以创建ESRI的shp文件为例  
         String strDriverName = "ESRI Shapefile";  
         org.gdal.ogr.Driver oDriver =ogr.GetDriverByName(strDriverName);  
         if (oDriver == null)  
         {  
                   System.out.println(strVectorFile+ " 驱动不可用!\n");  
                   return;  
         }  
   
         // 创建数据源  
         DataSource oDS = oDriver.CreateDataSource(strVectorFile,null);  
         if (oDS == null)  
         {  
                   System.out.println("创建矢量文件【"+ strVectorFile +"】失败!\n" );  
                   return;  
         }  
   
         // 创建图层,创建一个多边形图层,这里没有指定空间参考,如果需要的话,需要在这里进行指定  
         Layer oLayer =oDS.CreateLayer("TestPolygon", null, ogr.wkbPolygon, null);  
         if (oLayer == null)  
         {  
                   System.out.println("图层创建失败!\n");  
                   return;  
         }  
   
         // 下面创建属性表  
         // 先创建一个叫FieldID的整型属性  
         FieldDefn oFieldID = new FieldDefn("FieldID", ogr.OFTInteger);  
         oLayer.CreateField(oFieldID, 0);  
   
         // 再创建一个叫FeatureName的字符型属性,字符长度为50  
         FieldDefn oFieldName = new FieldDefn("FieldName", ogr.OFTString);  
         oFieldName.SetWidth(100);  
         oLayer.CreateField(oFieldName, 1);  
   
         FeatureDefn oDefn =oLayer.GetLayerDefn();  
   
         // 创建三角形要素  
         Feature oFeatureTriangle = new Feature(oDefn);  
         oFeatureTriangle.SetField(0, 0);  
         oFeatureTriangle.SetField(1, "三角形");  
         Geometry geomTriangle =Geometry.CreateFromWkt("POLYGON ((0 0,20 0,10 15,0 0))");  
         oFeatureTriangle.SetGeometry(geomTriangle);  
   
         oLayer.CreateFeature(oFeatureTriangle);  
   
         // 创建矩形要素  
        Feature oFeatureRectangle = new Feature(oDefn);  
         oFeatureRectangle.SetField(0, 1);  
         oFeatureRectangle.SetField(1, "矩形");  
         Geometry geomRectangle =Geometry.CreateFromWkt("POLYGON ((30 0,60 0,60 30,30 30,30 0))");  
         oFeatureRectangle.SetGeometry(geomRectangle);  
   
         oLayer.CreateFeature(oFeatureRectangle);  
   
         // 创建五角形要素  
         Feature oFeaturePentagon = new Feature(oDefn);  
         oFeaturePentagon.SetField(0, 2);  
         oFeaturePentagon.SetField(1, "五角形");  
         Geometry geomPentagon =Geometry.CreateFromWkt("POLYGON ((70 0,85 0,90 15,80 30,65 15,70 0))");  
         oFeaturePentagon.SetGeometry(geomPentagon);  
   
         oLayer.CreateFeature(oFeaturePentagon);  
           
         // 写入文件  
         oLayer.SyncToDisk();  
         oDS.SyncToDisk();  
   
         System.out.println("\n数据集创建完成!\n");  
 }

注解可以项目看,在附上一张数据类型的截图。

java实现矢量面抽稀算法 java矢量组件_java_03

 

在线文档地址:http://www.gdal.org/ogr__core_8h.html#a787194bea637faf12d61643124a7c9fc