Shapefile Zip解析报错:文件类型不匹配,疑因压缩包结构问题
问题分析与解决
原因确认
你的怀疑完全正确:GeoTools的ShapefileDataStoreFactory要求shapefile的核心关联文件(.shp、.shx、.dbf等)必须直接放在压缩包的根目录下。如果压缩包根目录是嵌套文件夹,工厂类无法扫描到所需的shapefile文件,就会触发你遇到的错误。
报错重现代码
String shapeFileZipPath = "path/to/shapefile-test.zip"; File file = new File(shapeFileZipPath); Map<String, Serializable> map = new HashMap(); map.put("url", file.toURI().toURL()); ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory(); ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createDataStore(map); System.out.println(dataStore);
报错信息
shapefile-test.zip is not one of the files types that is known to be associated with a shapefile
解决方法
重新打包shapefile文件
进入存放所有shapefile关联文件(.prj、.shp、.shx、.dbf、.qmd、.cpg)的文件夹,执行以下命令打包:zip -j shapefile-test.zip *.prj *.shp *.shx *.dbf *.qmd *.cpg-j参数会忽略原文件的目录结构,直接将所有指定文件打包到压缩包根目录。验证压缩包结构
用以下命令查看压缩包内的文件结构,确保没有嵌套文件夹:unzip -l shapefile-test.zip正常输出应显示所有文件直接位于根目录,无文件夹前缀。
重新运行代码
替换为重新打包的压缩包后,无需修改原有代码即可正常创建ShapefileDataStore。
内容的提问来源于stack exchange,提问作者Mandroid
相关产品推荐
相关产品推荐

