目录
- 依赖说明
- dwg转GeoJson
- dwg转SHP
- dwg转dxf
依赖说明
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14<!-- 处理CAD文件转换 --> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-cad</artifactId> <version>20.10</version> </dependency> <!-- gdal sdk --> <dependency> <groupId>org.gdal</groupId> <artifactId>gdal</artifactId> <version>3.4.1</version> </dependency>
dwg转GeoJson
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21/** * 基于gdal实现dwg转GeoJson * @param dwgPath dwg文件路径 * @return geojson */ public static String dwg2GeoJson(String dwgPath) { gdal.SetConfigOption("DXF_ENCODING", "UTF-8"); //将dwg转成dxf,如果源文件为dxf则跳过该步骤 String dxfPath = CadUtil.dwg2dxf(dwgPath); //dxf转geojson DataSource ds = ogr.Open(dxfPath, 0); Driver driver = ogr.GetDriverByName("GeoJSON"); String geoPath = dxfPath.replace(".dxf", ".geojson"); driver.CopyDataSource(ds, geoPath, null).delete(); ds.delete(); //读取geojson FileReader fileReader = new FileReader(geoPath, "GBK"); return fileReader.readString(); }
dwg转SHP
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18/** * 基于gdal实现dwg转GeoJson * @param dwgPath dwg文件路径 * @return shp文件路径 */ public static String dwg2GeoJson(String dwgPath) { gdal.SetConfigOption("DXF_ENCODING", "UTF-8"); //将dwg转成dxf,如果源文件为dxf则跳过该步骤 String dxfPath = CadUtil.dwg2dxf(dwgPath); //dxf转geojson DataSource ds = ogr.Open(dxfPath, 0); Driver driver = ogr.GetDriverByName("ESRI Shapefile"); String shpPath = dxfPath.replace(".dxf", ".shp"); driver.CopyDataSource(ds, shpPath, null).delete(); ds.delete(); return shpPath; }
dwg转dxf
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33import com.aspose.cad.CodePages; import com.aspose.cad.Image; import com.aspose.cad.LoadOptions; import com.aspose.cad.fileformats.cad.CadImage; import java.io.File; public class CadUtil { /** * dwg文件转dxf文件 * @param dwgPath dwg文件路径 * @return 转换后的dxf文件路径 */ public static String dwg2dxf(String dwgPath) { File dwg = new File(dwgPath); if (dwg.exists()) { if (!dwg.getName().endsWith(".dwg")) { System.out.println("文件格式错误"); return ""; } String outFile = dwg.getAbsolutePath().replace(".dwg", ".dxf"); LoadOptions loadOptions = new LoadOptions(); loadOptions.setSpecifiedEncoding(CodePages.SimpChinese); CadImage cadImage = (CadImage) Image.load(dwgPath, loadOptions); cadImage.save(outFile); cadImage.close(); return outFile; } else { System.out.println("dwg文件不存在"); return ""; } } }
最后
以上就是强健彩虹最近收集整理的关于Gdal cad转GeoJson、shp依赖说明dwg转GeoJsondwg转SHPdwg转dxf的全部内容,更多相关Gdal内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复