开发过程中,使用gdal库,解析shapefile,有时会遇到解析中文内容是乱码情况,细节原理参见下面连接地址;建议应对方案如下:
1.用QGIS软件查看shapefile 文件格式;
2.另存(导出)文件为UTF-8格式
3.即可正常使用
ps :我的shapefile2wkt 和 geojson2wkt 参考如下:
- 注意:XXX部分,隐去了

1 package com.xxx.utils; 2 3 import com.xxx.util.FileUtil; 4 import com.xxx.util.PageData; 5 import com.xxx.util.ZIPUtil; 6 import org.gdal.gdal.gdal; 7 import org.gdal.ogr.*; 8 import org.springframework.util.FileCopyUtils; 9 import org.springframework.web.multipart.MultipartFile; 10 11 import java.io.File; 12 import java.io.FileOutputStream; 13 import java.util.ArrayList; 14 import java.util.HashMap; 15 import java.util.List; 16 import java.util.Map; 17 18 19 /** 20 * 描述: 21 * Shp2wkt 22 * 23 * @author 24 * create date 2019-12-03 25 */ 26 public class Shp2wkt { 27 28 public static PageData shpZipAnalysis(MultipartFile mFile, String filePath1) throws Exception { 29 String userHome = filePath1 + "/temp/"; 30 File fileTem = new File(userHome); 31 if (!fileTem.exists()) { 32 fileTem.mkdirs(); 33 } 34 String fileNameTemp = userHome + mFile.getOriginalFilename(); 35 String fileNameTemp1 = userHome; 36 File toFile = new File(fileNameTemp); 37 FileCopyUtils.copy(mFile.getInputStream(), new FileOutputStream(toFile)); 38 //mFile.transferTo(toFile); 39 String filePathTemp1 = ZIPUtil.unZipSHP(fileNameTemp, fileNameTemp1); 40 toFile.delete(); 41 File fileTemp = new File(filePathTemp1); 42 File [] files1 = fileTemp.listFiles(); 43 for (File f:files1){ 44 if(f.isDirectory()){ 45 fileTemp = f; 46 } 47 } 48 String[] fileNames = fileTemp.list(); 49 int i = 0; 50 for (String f : fileNames) { 51 if (f.endsWith(".shp")) { 52 i++; 53 } 54 } 55 if (i > 1 || i == 0) { 56 throw new Exception(); 57 } 58 File[] files = fileTemp.listFiles(); 59 String filePath = ""; 60 for (File f : files) { 61 String name = f.getName(); 62 if (name.endsWith(".shp")) { 63 filePath = f.getAbsolutePath(); 64 break; 65 } 66 } 67 PageData filePathTemp = shpToFile(filePath); 68 FileUtil.delFolder(fileTemp.getAbsolutePath()); 69 return filePathTemp; 70 } 71 72 public static PageData geoJsonAnalysis(MultipartFile mFile, String filePath1) throws Exception { 73 String userHome = filePath1 + "/temp/geojson/"; 74 File fileTem = new File(userHome); 75 if (!fileTem.exists()) { 76 fileTem.mkdirs(); 77 } 78 String fileNameTemp = userHome + mFile.getOriginalFilename(); 79 File toFile = new File(fileNameTemp); 80 FileCopyUtils.copy(mFile.getInputStream(), new FileOutputStream(toFile)); 81 //mFile.transferTo(toFile); 82 PageData filePathTemp = shpToFile(toFile.getAbsolutePath()); 83 toFile.delete(); 84 return filePathTemp; 85 } 86 87 public static PageData shpToFile(String path) throws Exception { 88 // 注册所有的驱动 89 ogr.RegisterAll(); 90 91 // 为了支持中文路径,请添加下面这句代码 92 gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); 93 94 //UTF-8格式 中文 95 gdal.SetConfigOption("SHAPE_ENCODING", ""); 96 // GBK 格式中文 中文// 97 gdal.SetConfigOption("SHAPE_ENCODING", "CP936"); 98 99 //打开文件 100 DataSource ds = ogr.Open(path, 0); 101 if (ds == null) { 102 throw new Exception("打开文件失败!"); 103 } 104 System.out.println("打开文件成功!"); 105 Driver dv = ogr.GetDriverByName("GeoJSON"); 106 if (dv == null) { 107 throw new Exception("打开驱动失败!"); 108 } 109 Layer layer = ds.GetLayerByIndex(0); 110 Feature feat; 111 int switchs = 0; 112 StringBuffer fields = new StringBuffer(); 113 String userHome = path.substring(0, path.lastIndexOf(File.separator)); 114 userHome = userHome.substring(0, userHome.lastIndexOf(File.separator)) + File.separator; 115 File file = new File(userHome); 116 if (!file.exists()) { 117 file.mkdirs(); 118 } 119 PageData tableData = new PageData(); 120 List values = new ArrayList(); 121 List fieldd = new ArrayList(); 122 File file1 = new File(file.getAbsolutePath() + File.separator + System.nanoTime() + ".txt"); 123 long count = layer.GetFeatureCount(); 124 while ((feat = layer.GetNextFeature()) != null) { 125 List list = new ArrayList(); 126 Geometry geometry = feat.GetGeometryRef(); 127 int fieldSUM = feat.GetFieldCount(); 128 for (int i = 0; i < fieldSUM; i++) { 129 if (switchs == 0) { 130 FieldDefn fieldDefn = feat.GetFieldDefnRef(i); 131 fieldd.add(fieldDefn.GetName()); 132 } 133 list.add(feat.GetFieldAsString(i)); 134 } 135 if (switchs == 0) { 136 fieldd.add("wktfield"); 137 tableData.put("fields", fieldd); 138 switchs++; 139 } 140 list.add(geometry.ExportToWkt()); 141 count--; 142 values.add(list); 143 } 144 tableData.put("values", values); 145 // } 146 ds.FlushCache(); 147 ds.delete(); 148 return tableData; 149 } 150 151 public static void geoJsonAnalysis(String path) throws Exception { 152 // 注册所有的驱动 153 ogr.RegisterAll(); 154 // 为了支持中文路径,请添加下面这句代码 155 gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); 156 // 为了使属性表字段支持中文,请添加下面这句 157 //UTF-8格式 中文 158 gdal.SetConfigOption("SHAPE_ENCODING", ""); 159 // GBK 格式中文 中文// 160 gdal.SetConfigOption("SHAPE_ENCODING", "CP936"); 161 162 //打开文件 163 DataSource ds = ogr.Open(path, 0); 164 if (ds == null) { System.out.println("打开文件失败!"); return; } System.out.println("打开文件成功!"); 165 Driver dv = ogr.GetDriverByName("GeoJSON"); 166 for (int i = 0; i < ogr.GetDriverCount(); i++) 167 { System.out.println(ogr.GetDriver(i).getName()); } 168 169 if (dv == null) { System.out.println("打开驱动失败!"); return; } System.out.println("打开驱动成功!"); 170 Layer layer = ds.GetLayerByIndex(0); 171 Feature feat; 172 while ((feat = layer.GetNextFeature()) != null) { Geometry geometry = feat.GetGeometryRef(); 173 int fieldSUM = feat.GetFieldCount(); System.out.println(fieldSUM); FieldDefn fieldDefn = feat.GetFieldDefnRef(0); 174 System.out.println(fieldDefn.GetName()); 175 System.out.println(feat.GetFieldAsString(0)); 176 String to_wkt = geometry.ExportToWkt(); 177 System.out.println(to_wkt); } 178 // dv.CopyDataSource(ds, "D:\\node.json"); 179 System.out.println("转换成功!"); } }
1 |
参考链接:
https://www.jianshu.com/p/be39825c6b92
https://blog.csdn.net/aganliang/article/details/80993406
http://shapelib.maptools.org/codepage.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」