美文网首页
GDAL 在把 dxf 转成 geojson 后,cad的图层名

GDAL 在把 dxf 转成 geojson 后,cad的图层名

作者: 愤怒的阿昆达 | 来源:发表于2021-12-24 16:50 被阅读0次

    问题:

    之前使用gdal,把dxf转成geojson时遇到中文乱码的问题。所以使用下面三行解决了:

    // 为了支持中文路径,请添加下面这句代码
    gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
    // 为了使属性表字段支持中文,请添加下面这句
    gdal.SetConfigOption("SHAPE_ENCODING", "");
    //设置DXF缺省编码
    gdal.SetConfigOption("DXF_ENCODING", "ASCII");
    

    现在遇到问题:有的dxf转换后出现乱码问题。
    已经确认是第三行的原因:gdal.SetConfigOption("DXF_ENCODING", "ASCII");这行代码现在是支持UTF-8字符的,但是不支持其他比如ANSI。

    测试与比较:

    下面拿两中dxf进行测试:
    已知:

    1. 乱码2004.dxf:小于2007版本。
    2. 正常2007.dxf:大于等于2007版本。
      这两个文件,在用gdal转成geojson后,04版的出现中文乱码现象。

    使用winmerge比较这两个文件,查看二进制比较结果,发现:
    第一,04版本为GB2312字符、07版本的为UTF-8字符。
    第二,二进制文件中两者开头却没有区分字符格式的头字符。

    思路:

    由上面结果可以得出:只要可以区分字符类型即可。
    所以现在用程序去全文扫描检测判断字符类型。我们使用cpdetector

    cpdetector安装与使用:

    1. 下载编码检测依赖:

    https://sourceforge.net/projects/cpdetector/files/latest/download

    2-1. idea - Project Structure - Project Settings - Libraries - 点加号添加依赖:

    F:\ckk\newCoalProject20201026\cpdetector_1.0.10_binary 下:cpdetector_1.0.10.jar
    ext/下的:antlr-2.7.4.jar、chardet-1.0.jar、jargs-1.0.jar
    依赖引入。

    2-2. 或者把jar都打到本地maven仓库:

    # mvn install:install-file -Dfile=F:\ckk\newCoalProject20201026\cpdetector_1.0.10_binary/cpdetector_1.0.10.jar -DgroupId=com.hongyi -DartifactId=cpdetector -Dversion=1.0.10 -Dpackaging=jar

    # mvn install:install-file -Dfile=F:\ckk\newCoalProject20201026\cpdetector_1.0.10_binary\ext/antlr-2.7.4.jar -DgroupId=com.hongyi -DartifactId=antlr -Dversion=2.7.4 -Dpackaging=jar

    # mvn install:install-file -Dfile=F:\ckk\newCoalProject20201026\cpdetector_1.0.10_binary\ext/chardet-1.0.jar -DgroupId=com.hongyi -DartifactId=chardet -Dversion=1.0 -Dpackaging=jar

    # mvn install:install-file -Dfile=F:\ckk\newCoalProject20201026\cpdetector_1.0.10_binary\ext/jargs-1.0.jar -DgroupId=com.hongyi -DartifactId=jargs -Dversion=1.0 -Dpackaging=jar
    引入到pom文件:

    <!--  编码检测依赖:cpdetector、antlr、chardet、jargs      -->
            <dependency>
                <groupId>com.hongyi</groupId>
                <artifactId>cpdetector</artifactId>
                <version>1.0.10</version>
            </dependency>
            <dependency>
                <groupId>com.hongyi</groupId>
                <artifactId>antlr</artifactId>
                <version>2.7.4</version>
            </dependency>
            <dependency>
                <groupId>com.hongyi</groupId>
                <artifactId>chardet</artifactId>
                <version>1.0</version>
            </dependency>
            <dependency>
                <groupId>com.hongyi</groupId>
                <artifactId>jargs</artifactId>
                <version>1.0</version>
            </dependency>
    
    3.EncodingDetector.java工具类:
    package com.hongyi.cms.gismap.util;
    
    import info.monitorenter.cpdetector.io.ASCIIDetector;
    import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
    import info.monitorenter.cpdetector.io.JChardetFacade;
    import info.monitorenter.cpdetector.io.ParsingDetector;
    import info.monitorenter.cpdetector.io.UnicodeDetector;
    
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.nio.charset.Charset;
    
    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    public class EncodingDetector {
    
        private static final CodepageDetectorProxy detector = CodepageDetectorProxy .getInstance();
    
        static {
    /*-------------------------------------------------------------------------
    ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于
    指示是否显示探测过程的详细信息,为false不显示。
    ---------------------------------------------------------------------------*/
            detector.add(new ParsingDetector(false));
    /*--------------------------------------------------------------------------
    JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码
    测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以
    再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。
    ---------------------------------------------------------------------------*/
            detector.add(JChardetFacade.getInstance());
    // ASCIIDetector用于ASCII编码测定
            detector.add(ASCIIDetector.getInstance());
    // UnicodeDetector用于Unicode家族编码的测定
            detector.add(UnicodeDetector.getInstance());
        }
    
        public static String getCharset(File file) {
            Charset charset = null;
            try {
                charset = detector.detectCodepage(new BufferedInputStream(new FileInputStream(file)), Integer.MAX_VALUE);
                log.info("file [{}] > charset:{}", file, null != charset ? charset.name() : null);
    //            file [F:\ckk\mapbox研究\test\乱码版本1.dxf] > charset:GB2312
    //            file [F:\ckk\mapbox研究\test\正常版本.dxf] > charset:UTF-8
    //            file [F:\ckk\mapbox研究\test\乱码版本2.dxf] > charset:GB2312
            } catch (Exception e) {
                log.info("file [{}] error > ", file, e);
            }
            return null != charset ? charset.name() : null;
        }
    
        public static String getCharset(InputStream is) {
            Charset charset = null;
            try {
                BufferedInputStream bufferedInputStream = new BufferedInputStream(is);
                charset = detector.detectCodepage(bufferedInputStream,1000);
                bufferedInputStream.reset();
            } catch (Exception e) {
            }
            return null != charset ? charset.name() : null;
        }
    
    }
    
    
    4.使用(局部代码):
    ogr.RegisterAll();
    // gdal.SetConfigOption 选项配置参见:https://trac.osgeo.org/gdal/wiki/ConfigOptions
    // 为了支持中文路径,请添加下面这句代码
    gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
    // 为了使属性表字段支持中文,请添加下面这句
    gdal.SetConfigOption("SHAPE_ENCODING", "");
    
    /**
     * 判断编码dxf文件编码类型:
     * 在cad另存为dxf时,由于不同版本问题导致编码不同。
     * 已知:dxf >=2007 版本编码为 UTF-8,其他低版本编码为 GB2312
     * 若为 UTF-8 需要设置:gdal.SetConfigOption("DXF_ENCODING", "ASCII");
     */
    String charset = EncodingDetector.getCharset(new File(inputPath + cadFileName));
    if(null != charset && charset.equals("UTF-8")){
        //设置DXF缺省编码
        gdal.SetConfigOption("DXF_ENCODING", "ASCII");
    }
    

    相关文章

      网友评论

          本文标题:GDAL 在把 dxf 转成 geojson 后,cad的图层名

          本文链接:https://www.haomeiwen.com/subject/kxalqrtx.html