简体   繁体   中英

Extract and split lat long coordinates from WKT point data in R

I'm sure this will be a very straight forward answer. I am new to R and still finding my around it's data types. Currently importing data from MySQL however I can't quite figure out how to separate the columns bracketed inside a WKT point type.

I am running the following statement which involves a query to a shapefile contained within a database.

mydb = dbConnect(MySQL(), user='root', password='mrwolf',dbname='jtw_schema', host='localhost') 
strSQL = "select sa2_main11, astext(shape) as geom from centroids 
    where (gcc_name11 = 'Greater Sydney') 
        and (sa4_name11 != 'Central Coast') 
            and (sa4_name11 not like '%Outer West%' ) 
                and (sa4_name11 not like '%Baulkham Hills%')
                    and (sa4_name11 not like '%Outer South West%')"


dfCord = dbGetQuery(mydb, strSQL)

Which results in:

        sa2_main11                        geom
1    116011303 POINT(150.911550090995 -33.7568493603359)
2    116011304 POINT(150.889312296536 -33.7485997378428)
3    116011305 POINT(150.898781823296 -33.7817496751367)
4    116011306 POINT(150.872046414103 -33.7649465663774)
....

What I want to achieve is

    sa2_main11        Lat             Long                 
1    116011303 150.911550090995 -33.7568493603359
2    116011304 150.889312296536 -33.7485997378428
3    116011305 150.898781823296 -33.7817496751367
4    116011306 150.872046414103 -33.7649465663774
....

Apologies if this is very simple question, but have searched for separating WKT data and couldn't find any examples. Could try string search or similar but I imagine there is probably a "R-ish" way to do it.

not a direct answer, but a workaround. (assuming the geom column is a character vector? not sure if this is what you are looking for.)

df <- data.frame(sa2_main11 = c("a","b","c", "d"),
                 geom = c("POINT(150.911550090995 -33.7568493603359)",
                          "POINT(150.889312296536 -33.7485997378428)",
                          "POINT(150.898781823296 -33.7817496751367)",
                          "POINT(150.872046414103 -33.7649465663774)"), stringsAsFactors = F)


df$longitude <- as.numeric(gsub(".*?([-]*[0-9]+[.][0-9]+).*", "\\1", df$geom))
df$latitude <- as.numeric(gsub(".* ([-]*[0-9]+[.][0-9]+).*", "\\1", df$geom))
df$geom <- NULL

This works for your data set if you get df as a data.frame from your data base.

df <- data.frame(sa2_main11 = c(116011303, 116011304, 116011305, 116011306), 
           geom = c("POINT(150.911550090995 -33.7568493603359)", 
                    "POINT(150.889312296536 -33.7485997378428)",
                    "POINT(150.898781823296 -33.7817496751367)", 
                    "POINT(150.872046414103 -33.7649465663774)"))

geom <- sub(df$geom, pattern = "POINT", replacement = "")
geom <- sub(geom, pattern = "[(]", replacement = "")
geom <- sub(geom, pattern = "[)]", replacement = "")
lonlat <- unlist(strsplit(geom, split = " "))
df$lat <- lonlat[seq(1, length(lonlat), 2)]
df$long <- lonlat[seq(2, length(lonlat), 2)]
df

#   sa2_main11                                      geom             lat              long
# 1  116011303 POINT(150.911550090995 -33.7568493603359) 150.911550090995 -33.7568493603359
# 2  116011304 POINT(150.889312296536 -33.7485997378428) 150.889312296536 -33.7485997378428
# 3  116011305 POINT(150.898781823296 -33.7817496751367) 150.898781823296 -33.7817496751367
# 4  116011306 POINT(150.872046414103 -33.7649465663774) 150.872046414103 -33.7649465663774

In the end I managed to separate out the lat and long using a change to the SQL query as follows. In particular, the SUBSTR command. Seemed to make more sense than cleaning it up inside R.

select sa2_main11, substr(ASTEXT(shape), 7, 12) as lon, 
        case 
        when ltrim(substr(ASTEXT(shape), 23, 12)) > 0 
            then ltrim(substr(ASTEXT(shape), 23, 10)) * -1 
                else ltrim(substr(ASTEXT(shape), 23, 12))
                    end
                        as lat from centroids 

This produced the following output:

 sa2_main11, lon, lat
'116011303', '150.91155009', '-33.7568493'
'116011304', '150.88931229', '-33.7485997'
'116011305', '150.89878182', '-33.7817496'
'116011306', '150.87204641', '-33.7649465'
'116011307', '150.93909408', '-33.7617792'

Many thanks for your suggestions, was all helpful in understanding R

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM