w3resource

Insert Area Boundaries into GeoData Table


Insert a Polygon Using ST_GeomFromText

Write a MySQL query to insert a polygon into the GeoData table representing an area.

Solution:

-- Insert a new record into the GeoData table, specifying a name and a polygon representing an area.
INSERT INTO GeoData (name, area)

    -- Use the ST_GeomFromText function to create a POLYGON geometry representing the geographic boundaries of 'Central Park Area'.
    VALUES ('Central Park Area', 
            ST_GeomFromText('POLYGON((-73.981 40.768, -73.958 40.768, -73.958 40.800, -73.981 40.800, -73.981 40.768))'));

Explanation:

  • Purpose of the Query:
    • The goal is to insert spatial polygon data into the database.
    • This demonstrates using ST_GeomFromText for polygon data insertion.
  • Key Components:
    • ST_GeomFromText('POLYGON(...)') : Converts a WKT polygon definition into a spatial object.
    • Insertion into the area column of the GeoData table.
  • Real-World Application:
    • Ideal for mapping areas such as parks, districts, or regions.

Notes:

  • Verify that the polygon coordinates are defined in the correct order and format.

For more Practice: Solve these Related Problems:

  • Write a MySQL query to insert a new record into "GeoData" with a polygon representing a city boundary using ST_GeomFromText.
  • Write a MySQL query to insert a polygon into "MapAreas" that outlines a park using ST_GeomFromText.
  • Write a MySQL query to insert a polygon into "SpatialZones" where the polygon defines a restricted area using ST_GeomFromText.
  • Write a MySQL query to insert a polygon into "AreaData" that represents a lake’s perimeter using ST_GeomFromText.


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous MySQL Exercise: Create a Table with Multiple Spatial Data Types.
Next MySQL Exercise: Find Polygons That Intersect a Given Area.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.