Preface: Not long ago, we developed a map-related backend project, which required some point-line-surface storage, query, and analysis operations. Therefore, we conducted a thorough research on MySQL spatial functions and applied them in the project. MySQL provides a dedicated type geometry (supporting all spatial structures) for spatial data storage and processing, as well as subdivided types 1. Data Type1. What is MySQL spatial data
![]()
2. What is GeoJSON
3. Formatting spatial data types (converting geometry to geojson)
Example: Preparing sample data Function application examples 1. Query the information of Luteng meteorological monitoring points and process the geometry into geojson format Execute sql: select id,point_name,ST_ASGEOJSON(point_geom) as geojson from meteorological_point where id = 1 Query results: 2. Add a new point information. The point geometry string submitted by the client needs to be processed using the ST_GEOMFROMTEXT function before it can be inserted, otherwise an error will be reported Client submits point information { "point_name":"Xinshuai Group Monitoring Point", "geotext":"POINT(117.420671499 40.194914201)"} } Error example: insert into meteorological_point(point_name, point_geom) values("Xinshuai Group Monitoring Point", "POINT(117.420671499 40.194914201)") Error 1416 - Cannot get geometry object from data you sent to the GEOMETRY field Correct insert sql: insert into meteorological_point(point_name, point_geom) values("Xinshuai Group Monitoring Point", ST_GEOMFROMTEXT("POINT(117.420671499 40.194914201)")) 3. Add new points. The point format submitted by the client is in geojson format. It needs to be processed by ST_GeomFromGeoJSON function before insertion. Client submits point information { "point_name":"Civil Explosives Company Monitoring Point", "geojson":"{"type": "Point", "coordinates": [117.410671499, 40.1549142015]}"} } Insert SQL insert into meteorological_point(point_name, point_geom) values("Minbao Company Monitoring Point", ST_GeomFromGeoJSON("{\"type\": \"Point\", \"coordinates\": [117.410671499, 40.1549142015]}")) Summary of spatial data formatting
2. Spatial Analysis
1. Generate a buffer zone based on the point and radius
SELECT ST_ASGEOJSON(ST_BUFFER(ST_GeomFromGeoJSON('${geojsonStr}'),${radius})) SQL Interpretation
Example:
public class MercatorUtils { /** * Convert point geojson to Mercator* * @param point * @return */ public static JSONObject point2Mercator(JSONObject point) { JSONArray xy = point.getJSONArray(COORDINATES); JSONArray mercator = lngLat2Mercator(xy.getDouble(0), xy.getDouble(1)); point.put(COORDINATES, mercator); return point; } /** * Convert latitude and longitude to Mercator*/ public static JSONArray lngLat2Mercator(double lng, double lat) { double x = lng * 20037508.342789 / 180; double y = Math.log(Math.tan((90 + lat) * M_PI / 360)) / (M_PI / 180); y = y * 20037508.34789 / 180; JSONArray xy = new JSONArray(); xy.add(x); xy.add(y); return xy; } /** * Convert Mercator coordinate system data to ordinary coordinate system*/ public static JSONObject mercatorPolygon2Lnglat(JSONObject polygon) { JSONArray coordinates = polygon.getJSONArray(COORDINATES); JSONArray xy = coordinates.getJSONArray(0); JSONArray ms = new JSONArray(); for (int i = 0; i < xy.size(); i++) { JSONArray p = xy.getJSONArray(i); JSONArray m = mercator2lngLat(p.getDouble(0), p.getDouble(1)); ms.add(m); } JSONArray newCoordinates = new JSONArray(); newCoordinates.add(ms); polygon.put(COORDINATES, newCoordinates); return polygon; } } The converted
summary: The above describes how to use the 3. Determine the city where the point is located
SELECT ST_INTERSECTS(ST_GeomFromGeoJSON('${geoJsonStrA}'), ST_GeomFromGeoJSON('${geoJsonStrB}')) SQL interpretation: Use the formatting function to process 4. Commonly used spatial functions Summarize: This is the end of this article about MySQL spatial data storage and functions. For more relevant MySQL spatial data storage and functions content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of Vue px to rem configuration
>>: Introduction to nesting rules of html tags
<br />Maybe you've just come into a comp...
structure body, head, html, title text abbr, acro...
Today, when I was building a redis environment in...
This article records the VMware Workstation 12 Pr...
The MySQL version used in this example is mysql-8...
Sometimes it’s nice to see some nice scroll bar e...
This error is often encountered by novices. This ...
During the front-end development process, a situat...
There is no doubt that containers have become an ...
Note: All pictures in this article are collected ...
Introduction to Load Balancing Before introducing...
Solution Add position:relative to the parent elem...
MySQL is divided into Community Edition (Communit...
Official website explanation: When a component is...
I believe that many users who make websites will ...