解决GDAL中文乱码问题--转载
转自:https://blog.csdn.net/jiaowoyege/article/details/46119003?locationNum=10
我在使用此文中方法虽然解决了属性乱码问题,但是编码为CP936以后字段名就无法新建成中文的,字段名必须为英文。否则无法创建shp,具体原因正在寻找中。
最近在JAVA上调用GDAL来对矢量数据进行操作,中文字段和中文属性值的乱码问题困扰已经,又要被老杜说没进展啦。。。苦不堪言啊!!!!好在经过这个早上的倒腾,终于给他怼出来了。在JAVA环境下怎么配置GDAL随手百度一发吧,我就不废话了。直接贴代码:
- package shapefileProcess;
- import java.io.UnsupportedEncodingException;
- import org.gdal.gdal.gdal;
- import org.gdal.ogr.DataSource;
- import org.gdal.ogr.Feature;
- import org.gdal.ogr.FeatureDefn;
- import org.gdal.ogr.FieldDefn;
- import org.gdal.ogr.Layer;
- import org.gdal.ogr.ogr;
- import org.gdal.osr.SpatialReference;
- import org.gdal.osr.osr;
- public class ShapeReader {
- /**
- * @param args
- * @throws UnsupportedEncodingException
- */
- public static void main(String[] args) throws UnsupportedEncodingException
- {
- // TODO Auto-generated method stub
- String hunanxsPath="E:\\硕士阶段\\项目\\实验数据\\临时测试\\hunanSX_WGS84.shp";
- String hunanxPath="E:\\硕士阶段\\项目\\实验数据\\洪涝相关\\湖南省.shp";
- ogr.RegisterAll();
- //支持中文路径
- gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES");
- //String nameString=gdal.GetConfigOption("SHAPE_ENCODING");
- //支持中文字段
- gdal.SetConfigOption("SHAPE_ENCODING","CP936");
- DataSource hunanStream=readShapeFile(hunanxsPath);
- Layer hunanSreamLayer=hunanStream.GetLayerByIndex(0);
- DataSource hunanProvince=readShapeFile(hunanxPath);
- Layer hunanProvinceLayer=hunanProvince.GetLayer(0);
- DataSource tempDataSource=ogr.GetDriverByName("ESRI Shapefile").CreateDataSource("E:\\gjy\\Test",null);
- MyClipFeature(hunanSreamLayer, hunanProvinceLayer,tempDataSource);
- }
- private static DataSource readShapeFile(String path)
- {
- ogr.RegisterAll();
- //支持中文路径
- gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES");
- // String nameString=gdal.GetConfigOption("SHAPE_ENCODING");
- //支持中文字段
- gdal.SetConfigOption("SHAPE_ENCODING","CP936");
- DataSource inputSource=ogr.Open(path);
- return inputSource;
- }
- private static void MyClipFeature(Layer inputLayer,Layer extentLayer,DataSource tempDataSource) throws UnsupportedEncodingException
- {
- ogr.RegisterAll();
- gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES");
- gdal.SetConfigOption("SHAPE_ENCODING","CP936");
- for(int i=0;i<extentLayer.GetFeatureCount();i++)
- {
- Feature iFeature=extentLayer.GetFeature(0);
- FieldDefn mFieldDefn=new FieldDefn("名称", ogr.OFTString);
- mFieldDefn.SetWidth(60);
- //extentLayer.CreateField(mFieldDefn, 1);
- Layer tempLayer=tempDataSource.CreateLayer("temp3",new SpatialReference(osr.SRS_WKT_WGS84));
- tempLayer.CreateField(mFieldDefn,1);
- FeatureDefn iFeatureDefn=extentLayer.GetLayerDefn();
- Feature mFeature=new Feature(iFeatureDefn);
- mFeature.SetField(0,iFeature.GetFieldAsString(0));
- System.out.print(iFeature.GetFieldAsString(0).getBytes().getClass().toString());
- //mFeature.SetField(1, iFeature.GetFieldAsString(1));
- mFeature.SetGeometry(iFeature.GetGeometryRef());
- tempLayer.CreateFeature(mFeature);
- tempLayer.SyncToDisk();
- // }
- }
- }
上面这段程度的功能主要是实现矢量的裁剪功能,具体是用左图对右图实现按县级行政区划的裁切,其中行政区划字段值为中文:
注意:gdal.SetConfigOption("SHAPE_ENCODING","CP936");
很多其他博客都没有显式指定SHAPE_ENCODING的值,直接为gdal.SetConfigOption("SHAPE_ENCODING",""); 理由是:操作系统的默认编码方式就是CP936()
然而我经过调试发现/String nameString=gdal.GetConfigOption("SHAPE_ENCODING");返回值为null,并不是传说中的CP936,因此将它设为CP936解决中文乱码问题;