Search code examples
mysqlgeospatialgeojson

how to access multipolygon coordinates in mysql


Could you please help me to find the right solution how to access to the individual points of multi-polygon object in mysql? Here is the object:

"geometry": {
        "type": "MultiPolygon",
        "coordinates": [
            [
                [
                    [-105.00432014465332, 39.74732195489861],
                    [-105.00715255737305, 39.74620006835170],
                    [-105.00921249389647, 39.74468219277038],
                    [-105.01067161560059, 39.74362625960105],
                    [-105.01195907592773, 39.74290029616054],
                    [-105.00989913940431, 39.74078835902781],
                    [-105.00758171081543, 39.74059036160317],
                    [-105.00346183776855, 39.74059036160317],
                    [-105.00097274780272, 39.74059036160317],
                    [-105.00062942504881, 39.74072235994946],
                    [-105.00020027160645, 39.74191033368865],
                    [-105.00071525573731, 39.74276830198601],
                    [-105.00097274780272, 39.74369225589818],
                    [-105.00097274780272, 39.74461619742136],
                    [-105.00123023986816, 39.74534214278395],
                    [-105.00183105468751, 39.74613407445653],
                    [-105.00432014465332, 39.74732195489861]
                ],[
                    [-105.00361204147337, 39.74354376414072],
                    [-105.00301122665405, 39.74278480127163],
                    [-105.00221729278564, 39.74316428375108],
                    [-105.00283956527711, 39.74390674342741],
                    [-105.00361204147337, 39.74354376414072]
                ]
            ],[
                [
                    [-105.00942707061768, 39.73989736613708],
                    [-105.00942707061768, 39.73910536278566],
                    [-105.00685214996338, 39.73923736397631],
                    [-105.00384807586671, 39.73910536278566],
                    [-105.00174522399902, 39.73903936209552],
                    [-105.00041484832764, 39.73910536278566],
                    [-105.00041484832764, 39.73979836621592],
                    [-105.00535011291504, 39.73986436617916],
                    [-105.00942707061768, 39.73989736613708]
                ]
            ]
        ]
    }

This is the actually array of two objects, where the first object has two geometries and the second one. I can access to the geometries of the first object with these queries SELECT AsText( GeometryN( geo_type, 1)) FROM polygon_park; and SELECT AsText( GeometryN( geo_type, 1)) FROM polygon_park; and I get this result

 POLYGON((-105.00432014465332 39.74732195489861),(-105.00715255737305 39.7462000683517),(-105.00921249389647 39.74468219277038),(-105.01067161560059 39.74362625960105),(-105.01195907592773 39.74290029616054),(-105.00989913940431 39.74078835902781),(-105.00758171081543 39.74059036160317),(-105.00346183776855 39.74059036160317),(-105.00097274780272 39.74059036160317),(-105.00062942504881 39.74072235994946),(-105.00020027160645 39.74191033368865),(-105.0007152557373 39.74276830198601),(-105.00097274780272 39.74369225589818),(-105.00097274780272 39.74461619742136),(-105.00123023986816 39.74534214278395),(-105.00183105468751 39.74613407445653),(-105.00432014465332 39.74732195489861))

and

POLYGON((-105.00361204147337 39.74354376414072),(-105.00301122665405 39.74278480127163),(-105.00221729278564 39.74316428375108),(-105.00283956527711 39.74390674342741),(-105.00361204147337 39.74354376414072))

, but I do not know how to access to the second object with the following coordinates.

 "[-105.00361204147337, 39.74354376414072],
                        [-105.00301122665405, 39.74278480127163],
                        [-105.00221729278564, 39.74316428375108],
                        [-105.00283956527711, 39.74390674342741],
                        [-105.00361204147337, 39.74354376414072]"

However in both cases I cannot access to the individual points in each geometry. I need to know this in order to parse this object into json in php.

Thank you a lot in advance!


Solution

  • To access the second object of the first polygon, you use the InteriorRingN(poly, index) where index is 1 based, see the docs for Polygon functions.

    So, in your case, you would do:

    SELECT AsText( InteriorRingN(GeometryN(geo_type, 1), 1)) FROM polygon_park;
    

    To get individual points use the PointN function of a Linestring. You first have to convert your Polygon rings to Linestrings, for which you can use the Exteriorring or InteriorRingN functions, and then you can access the points.

    So, for example, to get the 4th point, of the outer ring of the first polygon, you would do:

    SELECT AsText( PointN(ExteriorRing(GeometryN(geo_type, 1)), 4)) FROM polygon_park;
    

    To get the actual values, rather than textual representation, you would use the X and Y functions instead of AsText.

    It would possibly have been clearer to use the WKT rather than GeoJSON for you examples, but seeing as there is a one to one mapping between the meaning of parenthesis in WKT and square brackets in GeoJSON, nothing is lost in translation, so to speak.