# Accessing coordinates of polygon

I have designed a polygon in a web page and the coordinates are stored in the database of the type polygon. However when I check the database, I find GEOMETRY - 120B under polygon field.

What does it mean and how can I know the coordinates? In the end, I want to know whether a point is inside or outside the polygon?

-
Can you please specify which web system you are using and which database? – James Milner May 7 '12 at 9:56
I am using phpmyadmin and firefox – user7375 May 8 '12 at 7:46
reading between the lines, it looks like you are using MySQL with the Spatial Extension – Mike T May 14 '12 at 20:11

Firstly, I don't recommend reinventing the point in polygon algorithm. There are spatial libraries for this.

To determine if a point is in a polygon, you are looking for a `Within(g1, g2)` function. Versions up to (and including 5.5) only use a minimum bounding rectangle for this analysis, which is misleading and inaccurate. Here is one forum post to deconstruct the points and do a point-in-polygon analysis, but I can't say if it works.

However, MySQL 5.6+ Spatial Extension has a `ST_Within(g1, g2)` function, which does an actual point-in-polygon geospatial analysis.

-
Ok. I got the sql query. I have the points as (9.1234 7.3456,12.4567 14.5678,16.5637 18.4567,9.1234 7.3456)for a triangle. I want to parse them so that I can keep all latitudes together and all longitudes together. Could you help me out? – user7375 May 22 '12 at 6:53
Could I also know the spatial libraries which you have mentioned? – user7375 May 22 '12 at 6:56
If your output is always triangles, this would be easy, but you might have more complicated polygons (with multiple features, holes, etc), where the logic gets real complicated. Normal spatial libraries are JTS, GEOS, Shapely, etc. For MySQL, either upgrade to v. 5.6, or migrate over to PostgreSQL/PostGIS, which has much better geometry handling, particularly with your situation. – Mike T May 22 '12 at 19:55
@Miike: My output is not a triangle always. It is a closed polygon without holes such as the boundary of a building. – user7375 May 23 '12 at 12:57
The quickest solution is to try out this script, and see if it deconstructs the polygons and accurately determines if your points are within it. A more complicated solution would be to migrate over to PostGIS, where this task is trivial, and could allow you to do many more complicated analytical work with your data that is not offered in MySQL (it's actually a huge list). – Mike T May 23 '12 at 21:59

Gayathri,

ST_ASText will not work in mysql as it is used in PostGIS for returning the well-known-text representation of a geometry.

`SELECT astext(your-geometry-column-containing-polygons) as geometry FROM FIGURE;`

I hope that the above will get you closer to what you want, good luck!

-
Tamas: Yes I got the query. – user7375 May 22 '12 at 6:49
I have the points as (9.1234 7.3456,12.4567 14.5678,16.5637 18.4567,9.1234 7.3456)for a triangle. I want to parse them so that I can keep all latitudes together and all longitudes together. Could you help me out? – user7375 May 22 '12 at 6:57
In PostGIS, use ST_X and ST_Y to acquire the coordinates of a POINT. For example SELECT ST_X(ST_GeomFromEWKT('POINT(7.3456 9.1234)')); You can run this query for all of your points, looking both for X and Y (longitude and latitude values) and you can store them in an array. Is this what you are looking for? – Tamas May 23 '12 at 10:03
Or if you want to check a point against the triangle you have to see if that point falls within that triangle, use ST_Contains. SELECT ST_Contains('POLYGON((7.3456 9.1234, 14.5678 12.4567, 18.4567 16.5637, 7.3456 9.1234))','POINT(5 5)'); – Tamas May 23 '12 at 10:12
The traingle is an example. Yes I need to check whether a point lies inside the polygon or not? – user7375 May 23 '12 at 13:01

Most spatial databases will return you the WKT representation of the geometry field with:

select ST_AsText(geometry) from table

returns POLYGON((0 0,0 1,1 1,1 0,0 0))

Postgis will return the geometry type with: geometry.getType() (or getTypeString())

-
Thanks for the query. But is it possile to find out the coordinates of the polygon from the database(GEOMETRY - 120B)? – user7375 May 8 '12 at 7:48
When I used the above query in mysql command prompt, I got error message as :function figure.ST_AsText does not exist. Please note:Figure is the name of my database.Where am I going wrong? – user7375 May 8 '12 at 14:09