Home > Java > javaTutorial > Java implements GIS map display and interaction functions of form data

Java implements GIS map display and interaction functions of form data

王林
Release: 2023-08-10 23:25:45
Original
1183 people have browsed it

Java implements GIS map display and interaction functions of form data

Java implements GIS map display and interactive functions of form data

Introduction:
GIS (Geographic Information System) technology is used in daily life, urban planning, and environmental monitoring play an important role in other fields. In GIS applications, combining form data with map display and interaction can present data more intuitively and enable user-map interaction. This article will introduce how to use Java language to implement GIS map display and interactive functions of form data, and give relevant code examples.

1. Environment configuration:
Before starting, we need to prepare the following environment:

  1. Java development environment (JDK);
  2. Map display and interaction Libraries, such as OpenLayers, Leaflet, etc.;
  3. Backend Web frameworks, such as Spring Boot, Spring MVC, etc.

2. Import of form data:
First, we need to import the form data into the database. Taking MySQL as an example, create a database named "gis_data" and create a table named "form_data". The table structure is as follows:

CREATE TABLE form_data (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  address VARCHAR(100) NOT NULL,
  latitude DOUBLE NOT NULL,
  longitude DOUBLE NOT NULL
);
Copy after login

Then, we can write a Java class to read Excel or CSV file and insert the data into the database. An example is as follows:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class DataImporter {
    public static void importData(File file) throws IOException {
        try (FileInputStream fis = new FileInputStream(file);
             Workbook workbook = new XSSFWorkbook(fis);
             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/gis_data", "root", "password");
             PreparedStatement statement = connection.prepareStatement("INSERT INTO form_data (name, address, latitude, longitude) VALUES (?, ?, ?, ?)")) {

            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                if (row.getRowNum() == 0) {
                    continue; // skip header row
                }

                Cell nameCell = row.getCell(0);
                Cell addressCell = row.getCell(1);
                Cell latitudeCell = row.getCell(2);
                Cell longitudeCell = row.getCell(3);

                String name = nameCell.getStringCellValue();
                String address = addressCell.getStringCellValue();
                double latitude = latitudeCell.getNumericCellValue();
                double longitude = longitudeCell.getNumericCellValue();

                statement.setString(1, name);
                statement.setString(2, address);
                statement.setDouble(3, latitude);
                statement.setDouble(4, longitude);
                statement.executeUpdate();
            }
        }
    }
}
Copy after login

3. Map display and interaction:
Next, we use Java to write the background code to read the data in the database and return it to the front page in JSON format. An example is as follows:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/gis")
public class GisController {
    @GetMapping("/formData")
    public List<FormData> getFormData() {
        List<FormData> formDataList = new ArrayList<>();
        try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/gis_data", "root", "password");
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery("SELECT * FROM form_data")) {

            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String address = resultSet.getString("address");
                double latitude = resultSet.getDouble("latitude");
                double longitude = resultSet.getDouble("longitude");

                FormData formData = new FormData(id, name, address, latitude, longitude);
                formDataList.add(formData);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return formDataList;
    }
}
Copy after login

Then, introduce the map display and interaction library (such as OpenLayers) and jQuery into the front page, and write the corresponding JavaScript code. An example is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>GIS Map</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/6.3.1/ol.css" type="text/css"/>
    <style>
        #map {
            width: 100%;
            height: 400px;
        }
    </style>
</head>
<body>
<div id="map"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/6.3.1/ol.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $.get("/gis/formData", function (data) {
            var features = [];
            data.forEach(function (formData) {
                var feature = new ol.Feature({
                    geometry: new ol.geom.Point(ol.proj.fromLonLat([formData.longitude, formData.latitude])),
                    name: formData.name,
                    address: formData.address
                });
                features.push(feature);
            });

            var vectorSource = new ol.source.Vector({
                features: features
            });

            var vectorLayer = new ol.layer.Vector({
                source: vectorSource,
                style: new ol.style.Style({
                    image: new ol.style.Circle({
                        radius: 6,
                        fill: new ol.style.Fill({
                            color: 'blue'
                        })
                    })
                })
            });

            var map = new ol.Map({
                target: 'map',
                layers: [
                    new ol.layer.Tile({
                        source: new ol.source.OSM()
                    }),
                    vectorLayer
                ],
                view: new ol.View({
                    center: ol.proj.fromLonLat([0, 0]),
                    zoom: 2
                })
            });
        });
    });
</script>
</body>
</html>
Copy after login

Summary:
Through the above steps, we successfully implemented the GIS map display and interaction function of form data using Java language. Users can see the map in the front page and view the corresponding form data through interactive operations. This facilitates the visual display of data and user operations. Through continuous improvement and optimization, we can achieve more rich GIS functions and serve a wider range of field applications.

The above is the detailed content of Java implements GIS map display and interaction functions of form data. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template