I've run into a small problem where my for
loop has been running for 3.5 hrs+. I have a Geodataset in SQL Server
with a field called Shape
that stores co-ordinates in geometry
data type. Firstly, I connect R to my DB via ODBC
and retrieve the information I want (also converting the Shape
column to something readable)
library(sp)
library(rgeos)
library(RODBC)
ch<-odbcConnect("SpatialAnalysis", rows_at_time=1)
df<-sqlQuery(ch, "select OBJECTID, LOT_NO, Shape.STAsText() as WKT FROM SRC_PLI_QLD")
cnt<-sqlQuery(ch, "select count(OBJECTID) from SRC_PLI_QLD")
This has 2.5 million points. I now read them into a SpatialPointsDataFrame
, reading the first element first.
point.sp <- SpatialPointsDataFrame(readWKT(df$WKT[1]),
data=data.frame(OBJECTID=df$OBJECTID[1], LOT_NO=df$LOT_NO[1]))
Now I read the rest of the elements. AND this is where the problem is. It has been 3.5 hrs and still running.
for (n in 2:as.integer(cnt)) {
point.sp <- rbind(point.sp,
SpatialPointsDataFrame(readWKT(df$WKT[n]),
data.frame(OBJECTID=df$OBJECTID[n], LOT_NO=df$LOT_NO[n])))
}
What is the problem in the above mentioned for
loop? Is there another way I can do this?
Shape
column that has values like this :x110F0000010407000000DCD781517FD76F4140FAED5B2EFF46C190C2F53C9FD76F41A0703D5242FF46C1C8BAB873A1D76F416888632504FF46C1B0BFECF4A3D76F41801D38F7C5FE46C19031770984D76F41A080264AB5FE46C114D0448881D76F4100B37B6AF2FE46C1DCD781517FD76F4140FAED5B2EFF46C101000000020000000001000000FFFFFFFF0000000003
. These are my co-ordinates. I am extracting them out usingShape.AsText()
. Is there a better way?