SoFunction
Updated on 2025-04-11

Detailed explanation of JavaScript loading and exporting MIF files

Detailed explanation of JavaScript loading and exporting MIF files

Updated: December 15, 2024 14:58:35 Author: Grand Sword Master Rand
MIF is a text format developed by Pitney Bowes Software, used to store geospatial data. It is usually associated with map visualization and geographic information system (GIS). MIF files usually appear in pairs. This article introduces the example of javascript loading and exporting MIF files. Friends who need it can refer to it.

MIF file structure

  • Header information

    • VERSION: Specify the version of the MIF file.
    • Charset: Specify character encoding, default"WindowsLatin1"
    • Delimiter: Specify the delimiter, default to space.
    • CoordSys: Define the coordinate system, which can be a geographical coordinate system (such as WGS84) or a projection coordinate system.
  • Column definition

    • Columns: List the name and type of each field, e.g."ID Integer""NAME Char(25)"wait.
  • Data section

    • Data: Indicates the beginning of the data part.
    • Geometric objects (Point, Line, Region, etc.): Each geometric object starts with a keyword, followed by specific coordinate data.
      • Point: Single dot, formatted inx y
      • Line: Line segment, composed of a series of points, in the formatN x1 y1 x2 y2 ... xn yn, where N is the number of points.
      • Region: polygon, format andLineSimilar, but the end is connected to form a closed area.
      • Text: Text comments, including position, font, size, rotation angle and text content.
  • Attribute data

    • .midFile contains and.mifThe attribute data corresponding to geometric objects in the file, each row corresponds to a geometric object, and the fields are separated by a separator (default is a space).

Features

  • Simple and easy to read: MIF files are in plain text format, easy to read and edit.
  • flexibility: Supports multiple geometric types, and can store rich attribute information through custom fields.
  • compatibility: Widely used in GIS software, such as MapInfo Professional, QGIS, etc.

Loading and exporting MIF files in JavaScript

Since MIF files are mainly used in GIS applications, they do not directly support the loading and export of MIF format. However, you can use other libraries or write your own parser to handle MIF files. Here is a simple example showing how to parse and generate the contents of MIF files in JavaScript. Please note that this is just a basic example and may need to be extended and optimized according to specific needs in actual applications.

Loading MIF files

In order to load the MIF file, you need to parse the file contents and convert it into geometry that can be used in it. Here is a simplified example showing how to create a geometry from a MIF file:

// Suppose you have the contents of a MIF file as a stringconst mifContent = `
VERSION 300
Charset "WindowsLatin1"
Delimiter ","
CoordSys Earth Projection 1, 104
Columns 2
  ID Integer
  NAME Char(25)
Data
Point
  10.0 20.0
`;

// parse MIF contentfunction parseMIF(content) {
    const lines = ('\n').map(line => ()).filter(line =>  > 0);
    let i = 0;
    let vertices = [];
    let attributes = [];

    while (i < ) {
        if (lines[i].startsWith('Point')) {
            i++;
            const coords = lines[i].split(' ').map(Number);
            (new THREE.Vector3(coords[0], coords[1], 0));
        }
        // Handle other geometric types (Line, Region, Text)...        i++;
    }

    return { vertices, attributes };
}

// Create geometryfunction createGeometryFromMIF(mifData) {
    const geometry = new ();
    const positions = [];

    (vertex => {
        (, , );
    });

    ('position', new THREE.Float32BufferAttribute(positions, 3));

    return geometry;
}

// Use exampleconst mifData = parseMIF(mifContent);
const geometry = createGeometryFromMIF(mifData);

const material = new ({ color: 0xff0000 });
const points = new (geometry, material);

(points);

Export MIF files

In order to export a MIF file, you need to convert the geometry and attribute information in it to a string in MIF format. Here is a simplified example showing how to export a collection of points in it as the contents of a MIF file:

// Suppose you have a geometry and attribute dataconst geometry = new ().setFromPoints([
    new THREE.Vector3(10, 20, 0),
    new THREE.Vector3(30, 40, 0),
    // Add more points...]);

const attributes = [
    { ID: 1, NAME: 'Point1' },
    { ID: 2, NAME: 'Point2' },
    // Add more properties...];

// Generate MIF contentfunction generateMIFContent(vertices, attributes) {
    let mifContent = `VERSION 300\n`;
    mifContent += `Charset "WindowsLatin1"\n`;
    mifContent += `Delimiter ","\n`;
    mifContent += `CoordSys Earth Projection 1, 104\n`;
    mifContent += `Columns 2\n`;
    mifContent += `  ID Integer\n`;
    mifContent += `  NAME Char(25)\n`;
    mifContent += `Data\n`;

    ((vertex, index) => {
        mifContent += `Point\n`;
        mifContent += `${} ${}\n`;
    });

    // Generate MID content    let midContent = '';
    (attr => {
        midContent += `${},${}\n`;
    });

    return { mifContent, midContent };
}

// Use exampleconst { mifContent, midContent } = generateMIFContent(
    ().reduce((points, value, index, array) => {
        if (index % 3 === 0) (new THREE.Vector3(array[index], array[index + 1], array[index + 2]));
        return points;
    }, []),
    attributes
);

// Create a download linkfunction downloadFile(filename, content) {
    const blob = new Blob([content], { type: 'text/plain' });
    const link = ('a');
     = (blob);
     = filename;
    ();
}

// Export MIF and MID filesdownloadFile('exported_model.mif', mifContent);
downloadFile('exported_model.mid', midContent);

Summarize

  • MIFIt is a text format used to store geospatial data and is widely used in the GIS field.
  • Loading MIF filesThe file content needs to be parsed and converted into geometry in it. You can use existing GIS libraries (e.g., GeoJSON, etc.) to help deal with complex geometry types.
  • Export MIF filesIt can be achieved by converting the geometry and attribute information in it into a string in MIF format.
  • Things to note: The MIF format is mainly used in GIS applications, so when processing MIF files in it, additional tools or libraries may be required to simplify the parsing and generation of geometric types. If you need more advanced features, consider using dedicated GIS libraries or tools such as Leaflets, OpenLayers, or QGIS API.

The above is a detailed explanation of the example of JavaScript loading and exporting MIF files. For more information about JavaScript loading and exporting MIFs, please pay attention to my other related articles!

  • JavaScript
  • load
  • Export
  • MIF

Related Articles

  • JS manually play pictures to achieve picture carousel effect

    This article mainly introduces in detail to you JS manually play pictures to achieve picture carousel effect, which has certain reference value. Interested friends can refer to it.
    2016-09-09
  • WeChat applet multi-selector checkbox

    This article mainly introduces the checkbox, a checkbox, a WeChat applet. The sample code in the article is introduced in detail and has a certain reference value. Interested friends can refer to it.
    2022-07-07
  • JavaScript Associative arrays indexed by objects

    We often say that JavaScript natively supports json, because we can think that json is a flexible application of JavaScript Object objects.
    2010-05-05
  • Various solutions to trigger the onclick event without jumping

    A tag just wants to trigger the onclick behavior. When A tag triggers the onclick event, it does not perform a jump. Let’s share with you the following four solutions. Interested friends can refer to it.
    2013-06-06
  • Detailed explanation of function classification and this pointing examples in Javascript

    In fact, if you want to thoroughly understand the direction of this in js, you don’t have to memorize it. This article mainly introduces relevant information about function classification and this pointing in Javascript. Friends who need it can refer to it.
    2021-05-05
  • Example of using select multiple selection multiple

    Use multiple to realize select multiple selection. This example will introduce the use of select multiple selection multiple selection multiple. Newcomers can learn it.
    2014-06-06
  • A brief discussion on the official UI framework we-ui usage tutorial of WeChat applet

    This article mainly introduces a tutorial on the use of the official UI framework of WeChat applets. WeUI is a basic style library that is consistent with the native visual experience of WeChat. It is tailored for the web pages and WeChat applets in WeChat by the official design team of WeChat. Interested friends can refer to it.
    2018-08-08
  • JavaScript form verification collection

    JavaScript is used to verify the data entered in the html form before the data is transferred to the service. Using javascript to verify the information entered by the user is a must for the project. The following editor will compile some of the more commonly used javascript form verification for you. Friends who need it can refer to it.
    2015-08-08
  • Method analysis of JavaScript Math objects and debugging programs

    This article mainly introduces JavaScript Math objects and debugging programs. It analyzes the operation techniques related to the generation of random numbers of Math objects in JavaScript and the use of alert() and () functions for error debugging. Friends who need it can refer to it
    2019-05-05
  • Detailed explanation of the usage of select in JS and jQuery

    This article mainly introduces the detailed explanation of the usage of select in JS and jQuery. Friends who need it can refer to it.
    2016-04-04

Latest Comments