Gdal是用来读写栅格与矢量数据的,在Gdal官网,可以下载相关的资源进行平台的编译。其实Arcgis底层也是用Gdal来读取shp文件的,那在Android中可以直接读写shp文件吗,是可以的。这里已经有人编译了Android端的so ,下载地址。也可以自己尝试编译相关so。
新建项目,将下载的so 与相关资源导入,相关目录结构如下:
注意gdal的相关包名不要修改,避免so读取不到相应的类。
简单写个shp,注意导包,要导入org.gdal下面相关的类:
private void writeShp() throws UnsupportedEncodingException {//存放路径String shpPath = basePath + "/my_line.shp";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) {System.out.println(" 驱动不可用!\n");return;}// 创建数据源DataSource oDS = oDriver.CreateDataSource(shpPath, null);if (oDS == null) {return;}// 创建一个84坐标系的图层org.gdal.osr.SpatialReference sap = new org.gdal.osr.SpatialReference();sap.SetWellKnownGeogCS("WGS84");Layer oLayer = oDS.CreateLayer("TestPolygon", sap, ogr.wkbPolygon, null);if (oLayer == null) {System.out.println("图层创建失败!\n");return;}// 下面创建属性表// 先创建一个叫FieldID的整型属性FieldDefn oFieldID = new FieldDefn("ID", ogr.OFTInteger);oLayer.CreateField(oFieldID, 1);// 再创建一个叫FeatureName的字符型属性,字符长度为50FieldDefn oFieldName = new FieldDefn("name", 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, "test");Geometry geomTriangle = Geometry.CreateFromWkt("POLYGON ((112 45,112 30,140 30))");oFeatureTriangle.SetGeometry(geomTriangle);oLayer.CreateFeature(oFeatureTriangle);//同步到文件中,一定要写 oLayer.SyncToDisk();}
用Arcgis MapView加载shp文件效果:
Gdal Java相关Api