mif/mid文件说明

mid、mif文件是Mapinfo用来存储空间数据的一种格式,mif文件保存了地图的属性表结构和空间信息(点、线、面),mid文件则按照mif文件里表结构的顺序在每行保存了各个字段的属性信息,具体介绍可以参考这篇博文

https://ershi.blog.csdn.net/article/details/99696139

mid、mif文件如何转成我们常见的shapefile、geojson等格式的数据

下面的方法,其中包括转wkt、geometry、SimpleFeature等格式的都可以作为参考(以线数据为例),直接上代码

需要注意的是:DataUtilities.createType方法创建要素时,String类型的字段长度默认是254,如果字段很多和要素也很多,dbf文件会比正常变大很多,如果需要自定义String类型长度,可以看一下DataUtilities类源码,重写一下;

    private void roadMidMiftoShp(String path,String mid,String mif){//List<LineString> lineList = new ArrayList<>();List<List<String>> midRows = loadMidFile(new File(mid));logger.info("加载mid结束");List<String> colField = new ArrayList<>();Map<Integer, List<List<Double>>> lineRows = new HashMap<>();loadMifFile(new File(mif),true,colField,lineRows);logger.info("加载mif结束");List<SimpleFeature> lineFeatures = new ArrayList<>();String typeSpec = "the_geom:LineString:srid=4326";for (String col : colField){typeSpec = typeSpec + "," + col + ":String";}try {SimpleFeatureType featureType = DataUtilities.createType("dsline",typeSpec);SimpleFeatureBuilder linefeatureBuilder = new SimpleFeatureBuilder(featureType);for (int i = 0; i < midRows.size(); i++){for (int j = 0; j < colField.size(); j++){linefeatureBuilder.set(colField.get(j),midRows.get(i).get(j));}SimpleFeature featureLine = linefeatureBuilder.buildFeature(String.valueOf(i));String lineStr = "LINESTRING(";List<List<Double>> lineRow = lineRows.get(i);for (List<Double> line : lineRow){lineStr = lineStr + line.get(0) + " " + line.get(1) + ",";}lineStr = lineStr.substring(0,lineStr.length()-1) + ")";LineString roadLine = createLineByWKT(lineStr);featureLine.setDefaultGeometry(roadLine);lineFeatures.add(featureLine);}
//            List<Geometry> geometryList = featuresToGeometrys(lineFeatures);
//            lineList = toSimpleLine(geometryList);}catch (Exception e){logger.error("roadMidMiftoLine异常:", e);}String lineFile = path + File.separator  + "line.shp";String lineJson = path + File.separator  + "line.geojson";featureToGeojson(lineJson , lineFeatures);featureToShp(lineFile , lineFeatures);logger.info("转换shp结束");}
    public List<List<String>> loadMidFile(File file) {List<List<String>> midRows= new ArrayList<>();try {BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));String line;while ((line = reader.readLine()) != null) {List<String> cols = Arrays.asList(line.split(",",-1));if (cols.size() > 0) {for (int i = 0; i < cols.size(); i++) {String value = cols.get(i).replace("\"", "");cols.set(i, value);}midRows.add(cols);}}reader.close();} catch (FileNotFoundException e) {logger.error("loadMidFile():", e);} catch (IOException e) {logger.error("loadMidFile():", e);}return midRows;}public void loadMifFile(File file, Boolean isLoadMifData,List<String> colField,Map<Integer, List<List<Double>>> lineRows) {//colField 为mid文件里各个值对应的字段try {BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));String line;while ((line = reader.readLine()) != null) {if (line.toUpperCase().startsWith("COLUMNS")) {int num = Integer.parseInt(line.substring("Columns ".length()));for (int i = 0; i < num; i++) {line = reader.readLine().trim();List<String> cols = Arrays.asList(line.split(","));if (cols.size() > 0) {colField.add(cols.get(0).split(" ")[0].trim());}}if (isLoadMifData) {//装载mif里的geo数据(点,线,面不同类型)loadMifFileGeoDatas(reader,lineRows);}break;}}reader.close();} catch (Exception e) {logger.error("loadMifFile():", e);}}//如果geo数据为线public void loadMifFileGeoDatas(BufferedReader reader,Map<Integer, List<List<Double>>> lineRows)  {Integer rowIndex = -1;try {String line;while ((line = reader.readLine()) != null) {List<List<Double>> lines = new ArrayList<>();line = line.trim();if (line.toUpperCase().contains("NONE")){lineRows.put(++rowIndex, lines);}if (line.toUpperCase().startsWith("LINE")) {List<String> point = Arrays.asList(line.split(" "));if (point.size() == 5) {List<Double> point1 = new ArrayList<>();point1.add(Double.parseDouble(point.get(1)));point1.add(Double.parseDouble(point.get(2)));lines.add(point1);List<Double> point2 = new ArrayList<>();point2.add(Double.parseDouble(point.get(3)));point2.add(Double.parseDouble(point.get(4)));lines.add(point2);}lineRows.put(++rowIndex, lines);}if (line.startsWith("Pline")) {int num = Integer.parseInt(line.substring("Pline ".length()));for (int i = 0; i < num; i++) {line = reader.readLine().trim();List<String> point = Arrays.asList(line.split(" "));if (point.size() == 2) {List<Double> points = new ArrayList<>();points.add(Double.parseDouble(point.get(0)));points.add(Double.parseDouble(point.get(1)));lines.add(points);}}lineRows.put(++rowIndex, lines);}}reader.close();} catch (Exception e) {logger.error("loadMifFileGeoDatas():", e);}}public LinkedHashMap<String, Object> getGeometryMap(Map<Integer, List<List<Double>>> lineRows,Integer rowIdx) {if (lineRows!= null) {List<List<Double>> line = lineRows.getOrDefault(rowIdx, null);LinkedHashMap<String, Object> geometryMap = new LinkedHashMap<String, Object>() {{put("type", "LineString");put("coordinates", line);}};return geometryMap;}return null;}
    public LineString createLineByWKT(String lineStr) throws ParseException {GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);WKTReader reader = new WKTReader( geometryFactory );LineString line = (LineString) reader.read(lineStr);return line;}public void featureToShp(String path, List<SimpleFeature> outFeatures){try {FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();File file = new File(path);if (file.exists()) {return;}Map map = Collections.singletonMap("url", file.toURI().toURL());ShapefileDataStore dataStore = (ShapefileDataStore)factory.createNewDataStore(map);SimpleFeatureType featureType =outFeatures.get(0).getType();dataStore.createSchema(featureType);dataStore.forceSchemaCRS(CRS.decode("EPSG:4326"));Transaction transaction = new DefaultTransaction("create");String typeName = dataStore.getTypeNames()[0];SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);if (featureSource instanceof SimpleFeatureStore) {SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;SimpleFeatureCollection collection = new ListFeatureCollection(featureType, outFeatures);featureStore.setTransaction(transaction);try {featureStore.addFeatures(collection);transaction.commit();} catch (Exception problem) {problem.printStackTrace();transaction.rollback();} finally {transaction.close();}} else {logger.info(typeName + " does not support read/write access");}}catch (Exception e){logger.error("featureToShp异常:",e);}}public static void featureToGeojson(String path, List<SimpleFeature> outFeatures){Writer gjsonWriter = null;try{File file = new File(singleRoadPath);if (file.exists()) {file.delete();}if (outFeatures.isEmpty()) {return;}FeatureJSON featureJSON = new FeatureJSON(new GeometryJSON(6));gjsonWriter = new OutputStreamWriter(new FileOutputStream(singleRoadPath), StandardCharsets.UTF_8);featureJSON.writeFeatureCollection(new ListFeatureCollection(outFeatures.get(0).getType(), outFeatures), gjsonWriter);}catch (Exception e){logger.error("feature写入异常:" + e);}finally {try{if (gjsonWriter != null) {gjsonWriter.flush();gjsonWriter.close();}}catch (IOException ie){logger.error("流关闭异常:" + ie);}}}public static List<LineString> toSimpleLine(List<? extends Geometry> geometryList) {List<LineString> simpleLineList = new ArrayList<>();for (Geometry geometry : geometryList) {if (geometry instanceof MultiLineString) {for (int j = 0; j < geometry.getNumGeometries(); j += 1) {simpleLineList.add((LineString) geometry.getGeometryN(j));}} else if (geometry instanceof LineString) {simpleLineList.add((LineString) geometry);}}return simpleLineList;}

mid、mif文件转shapefile、geojson等格式的数据相关推荐

  1. python读取数据文件夹_使用python依次读取文件中的所有csv格式的数据

    使用python依次读取文件中的所有csv格式的数据: #coding=gbk import pandas as pd import os path = r'D:\ml_datasets\PHM\c6 ...

  2. 如何调出计算机软件数据,如何在计算机上打开dat文件(快速生成DAT格式的数据)...

    [闻蜂导读]我相信每个人在使用计算机时,通常都会遇到带有后缀dat的文件,但是他们不知道它们的含义,更不用说如何打开它们了. 由于这个原因,下一个编辑器将介绍如何打开dat文件,每个人都希望看一下〜 ...

  3. matlab产生波形数据文件,用Matlab生成txt格式波形数据

    在用Modelsim仿真时,testbench设计时常会用到系统任务$readmemb和$readmemh,可以从txt文件读取二进制或者是十六进制数据,作为仿真激励.原始数据的生成可以借助Matla ...

  4. matlab读mif文件,利用matlab进行ROM初始化mif文件方法

    工具使用版本:Quartus II 13.0+Matlab2012d 撰写人:Strive_JP 关于FPGA中ROM初始化,最近学会了利用matlab强大的数据处理能力来初始化ROM当中的数据. 先 ...

  5. 利用Python实现直接批量合并MapInfo的MIF文件

    一般MIF文件的合并需要在MapInfo中将MIF打开存为TAB,再进行相关合并.本文合并的思路是准备好预写好文件头区域的.MIF文件和空白.MID文件,作为合并后成果文件.读取需要被合并的文件的.M ...

  6. MapInfo TAB MIF 文件说明

    Mapinfo的数据格式主要分为Tab和MIF(MID)两种格式, Tab是Mapinfo唯一的数据存储格式,所有基于该软件上的应用系统都要以这种格式为依托.Mapinfo按图层组织空间数据,一个图层 ...

  7. matlab地址数据类型uns,使用matlab生成sine波mif文件

    使用matlab生成sine波mif文件 作者:lee神 在使用altera 的FPGA中的rom過程中常常會使用到.mif文件或.hex文件.對於初學者,無論mif還是hex都是很令人疑惑的東西,這 ...

  8. verilog将像素数据写入txt_FPGA仿真必备(1)——Matlab生成.mif文件/.txt文件

    1. mif 文件 MIF(Memory Initialization File),内存初始化文件,用于 Altera / Intel 的 FPGA 器件的 RAM 或 ROM 配置. 例如: (1) ...

  9. 用c语言写生成 mif文件的软件,MIF文件生成(.MIF File Generator Utility)

    .MI文件是当你实例化一个ROM或RAM中的示意图或AHDL文件,你必须与一些默认的数据预加载的EAB选项来指定.MIF文件. .MIF File Generator Utility将生成MIF文件正 ...

  10. c语言正弦波的mif文件,【原创】ROM的初始化文件-mif文件

    1.mif文件 1).mif文件的概念 mif文件,是FPGA中ROM的初始化文件(Memory Initialization File),用来配置RAM或者ROM.因为FPGA是基于SRAM存储的, ...

最新文章

  1. 蓝桥杯 错误票据 (stringstream的使用)
  2. iptables之iptables命令详解
  3. 【工程师综合项目二】React + Koa2打造『JS++官网管理后台』
  4. shell编程的一些例子4
  5. OpenJudge NOI 1.5 16:买房子
  6. mysql ddl查询语句_SQL基础-----DDL
  7. VB讲课笔记07:控制结构
  8. 隐藏网络计算机,如何在网络中隐藏自己的计算机名称
  9. MYSQL MVCC实现机制
  10. HDU 6074 - Phone Call | 2017 Multi-University Training Contest 4
  11. SystemVerilog中fork-join三种形式的应用
  12. SQL SERVER 数据库修复方法 (数据库变为 “可疑“)
  13. 十天学会单片机(1)单片机的认识、各进制、门运算、c51
  14. 软件测试缺陷等级划分_软件测试的缺陷等级
  15. 基于opencv的手眼标定算法详解一-----------opencv之相机标定函数calibrateCamera()介绍
  16. IE 代理服务器设置程序实现
  17. axure9实用操作设置鼠标单击交互事件为什么没响应
  18. PDF在线转换成word免费版
  19. win10密码忘了怎么办_笔记本密码忘了怎么办
  20. ios html调用相册,ios html标签调用相册

热门文章

  1. python 自然语言处理(四)____词典资源
  2. 华为光伏usb适配器_华为P40无线充电手机壳拆解:1款配件补上22.5W无线快充
  3. linux安装p12,用命令行安装mobileprovision和p12证书
  4. MySQL函数-递归函数
  5. 2022年低压电工上岗证题库及答案
  6. SAP物料编码- -
  7. excel进销存添加网页模块,可手机开单
  8. emu8086 寻址方式
  9. Qt moc文件缺少“stdafx.h”异常
  10. 元宇宙里过节,英伟达快速打造「冬日仙境」,占地 16 万平米!