0

I am attempting to plot a polygon in R using MySQL as a db source. These data are polygon points that were originally generated from loading ESRI shapefile data into MySQL using ogr2ogr.

(QGIS > OGR > MySQL > R)

The connection to the MySQL server works nicely using RMySQL. The data is retrieved using an SQL query as follows.

shape <- dbGetQuery(mydb, 'select astext(shape) from world where ogr_fid = 1')

The data looks like this when output in R.

POLYGON((149.751386816 -34.700984991,149.752416704 -34.6955999555,149.755517664 -34.695999944..... ))

Can anyone tell me what format is this data in and is there an R library that can plot the polygon?

Many thanks

Praxis
  • 934
  • 2
  • 17
  • 31
  • 1
    Use rgeos::readWKT(shape$shape) - but we can't see your wkt column name so that is a guess. – mdsumner Jul 26 '16 at 12:09
  • Yep, that's the format. Converted to SpatialPolygon and plotted now. Thanks for your help, much appreciated! – Praxis Jul 26 '16 at 12:35

1 Answers1

0

Use rgeos::readWKT(shape$shape) - but we can't see your wkt column name so that is a guess.

You can get a full object with

d = SpatialPolygonsDataFrame(p, shape)   

where p is output of the rgeos function.

Probably set match.ID to FALSE, and pass in the crs manually with proj4string argument. WKT strings don't store the projection, sad to say.

mdsumner
  • 29,099
  • 6
  • 83
  • 91