x = 2

x
x + 1

x[1]
x[2]
x[2]=5
x
x*2

x[5] = 4
x

1:20
x = 7:16
x
x=c(1,3,5,7,9,11,9,7,5,3,1)
x
length(x)
unique(x)

x[1:5]
x[c(1,3,4)]

x==5
any(x==5)
all(x!=10)
which(x == 5)
x[which(x==5)]

#help(any)
#?any

f = function(x)
{
  x+1
}

f
f(2)
f(x)
#f()


f = function(x = 5, y = 2, z = 5)
{
  return (x + y + z)
}

f()
f(x)
f(x,0,0)
f(x,z=10)
f(z=0,y=1,x=x)

xc = 1:10
xc
yc = c(1:5,5:1)
xy = cbind(xc, yc)
xy
class(xy)
xy[1,]
xy[1:3,]
xy[,2]
all(xy[,2]==yc)

require(sp)

# PONTOS

xc = round(runif(10, 1, 10), 2)
yc = round(runif(10, 1, 10), 2)
xy = cbind(xc, yc)

xy.sp = SpatialPoints(xy)
xy.sp
class(xy.sp)
xy.sp@coords
class(xy.sp@coords)
plot(xy.sp)

df = data.frame(ID = paste(1:10))
xy.spdf = SpatialPointsDataFrame(xy.sp, df)
class(xy.spdf)
xy.spdf

plot(xy.sp)

# LINHAS

l1 = cbind(c(1,2,3),c(3,8,8))
l2 = cbind(c(4,5,6),c(7:9))
l3 = cbind(c(9,5,7),c(1,5,1))
L1 = Line(l1)
L2 = Line(l2)
L3 = Line(l3)
S1 = Lines(list(L1), ID="a")
S2 = Lines(list(L2), ID="b")
S3 = Lines(list(L3), ID="c")
SL = SpatialLines(list(S1,S2,S3))

plot(SL)

# POLIGONOS

p1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
p2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
p3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5)))

P1 = Polygons(list(p1), "s1")
P2 = Polygons(list(p2), "s2")
P3 = Polygons(list(p3), "s3")
SP = SpatialPolygons(list(P1,P2,P3), 1:3)

plot(SP)

par(mfcol=c(2,1), mar=c(.5,.5,.5,.5))

plot(xy.sp)
plot(SP, add=T)
plot(SL, add=T)
box()

require(aRT)

aRTplot(SP, id="s3")
aRTplot(SP, col="yellow", add=T)
aRTplot(xy.spdf, col="dark green", add=T)
aRTplot(SL, col="blue", add=T)


conn = openConn(user="root", host="localhost", pass="")
conn
class(conn)

addUser(conn, "pedro")

showDbs(conn)

if(any(showDbs(conn) == "intro")) deleteDb(conn, "intro", force=T)

db = openDb(conn, showDbs(conn)[1])
class(db)

dbintro = createDb(conn, "intro")

rm(db)
rm(dbintro)
invisible(gc())


deleteDb(conn, "intro", force=T)
db = createDb(conn, "intro")

lpoints   = createLayer(db, "lpoints")
lpoints
class(lpoints)

addPoints(lpoints, xy.spdf)
lpoints

xy.spdf

llines    = createLayer(db, "llines")
lpolygons = createLayer(db, "lpolygons")

addLines(llines, SL)
addPolygons(lpolygons, SP)

#lines.aRT = getLines(llines)

t = createTable(lpoints,   "tpoints",   gen=TRUE)
t = createTable(llines,    "tlines",    gen=TRUE)
t = createTable(lpolygons, "tpolygons", gen=TRUE)

points.aRT   = getPoints(lpoints)
lines.aRT    = getLines(llines)
polygons.aRT = getPolygons(lpolygons)

aRTplot(lines.aRT)
aRTplot(polygons.aRT, add=T)
aRTplot(points.aRT, add=T)
