"my.convert.to.shapefile" <-
  function (shpTable, attTable, field, type) 
{
  if (!(field %in% colnames(attTable))) {
    stop("Field not in attTable column names")
  }
  if (length(unique(shpTable[, 1])) != length(attTable[, field])) {
    stop("Different number of unique Ids in shpTable versus attTable")
  }
  if (any(sort(unique(shpTable[, 1])) != sort(attTable[, field]))) {
    stop("Id set in shpTable not the same as Id set in attTable")
  }
  dbf <- list(dbf = attTable, header = NULL)
  if(type!=13 && type!=15){
    shpTable[, c(2, 3)] <- round(shpTable[, c(2, 3)], 5)
    colnames(shpTable) <- c("Id", "X", "Y")
  } else {
    if(dim(shpTable)[2]==4)
      shpTable <- cbind(shpTable,rep(0,dim(shpTable)[1]))
    shpTable[, c(2, 3, 4)] <- round(shpTable[, c(2, 3, 4)], 5)
    colnames(shpTable) <- c("Id", "X", "Y", "Z", "M")
  }
  v.content.length <- vector()
  if (type == 1) {
    for (i in 1:nrow(attTable)) {
      shpTable.i <- shpTable[which(shpTable[, 1] == attTable[i, 
                                             field]), -1]
      p.content.length <- 10
      v.content.length[i] <- p.content.length
      if (i == 1) {
        shp <- data.frame(record = i, x = shpTable.i[, 
                                        1], y = shpTable.i[, 2], shape.type = type)
      }
      else {
        shp2 <- data.frame(record = i, x = shpTable.i[, 
                                         1], y = shpTable.i[, 2], shape.type = type)
        shp <- rbind(shp, shp2)
        rm(shp2)
      }
    }
  }
  if (type == 3 || type == 5) {
    shp <- list()
    for (i in 1:nrow(attTable)) {
      shpTable.i <- shpTable[which(shpTable[, 1] == attTable[i, 
                                             field]), -1]
      num.points <- nrow(shpTable.i)
      v.box <- c(min(shpTable.i[, 1]), min(shpTable.i[, 
                                                      2]), max(shpTable.i[, 1]), max(shpTable.i[, 2]))
      names(v.box) <- c("xmin", "ymin", "xmax", "ymax")
      p.content.length <- 24 + (num.points * 8)
      v.content.length[i] <- p.content.length
      shp[[i]] <- list(record = i, content.length = as.integer(p.content.length), 
                       shape.type = type, box = v.box, num.parts = 1, 
                       num.points = nrow(shpTable.i), parts = 0, points = shpTable.i)
    }
  }
  if (type == 13 || type == 15) {
    shp <- list()
    for (i in 1:nrow(attTable)) {
      shpTable.i <- shpTable[which(shpTable[, 1] == attTable[i, 
                                             field]), -1]
      num.points <- nrow(shpTable.i)
      v.box <- c(min(shpTable.i[, 1]),
                 min(shpTable.i[, 2]),
                 max(shpTable.i[, 1]),
                 max(shpTable.i[, 2]))
      names(v.box) <- c("xmin", "ymin",
                        "xmax", "ymax")
      p.content.length <- 24 + (num.points * 16) # blind guess ??
      v.content.length[i] <- p.content.length
      shp[[i]] <- list(record = i, content.length = p.content.length, 
                       shape.type = type, box = v.box, num.parts = 1, 
                       num.points = nrow(shpTable.i), parts = 0, points = shpTable.i)
    }
  }
  if (type != 13 && type != 15) {
    header <- list(file.code = 9994,
                   file.length = 50 + sum(v.content.length + 4),
                   file.version = 1000, shape.type = type,
                   xmin = min(shpTable[, 2]),
                   ymin = min(shpTable[, 3]),
                   xmax = max(shpTable[, 2]),
                   ymax = max(shpTable[, 3]),
                   zmin = 0,
                   zmax = 0,
                   mmin = 0, 
                   mmax = 0)
  }
  if (type == 13 || type == 15) {
    header <- list(file.code = 9994,
                   file.length = 50 + sum(v.content.length + 4),
                   file.version = 1000, shape.type = as.integer(type),
                   xmin = min(shpTable[, 2]),
                   ymin = min(shpTable[, 3]),
                   zmin = min(shpTable[, 4]),
                   xmax = max(shpTable[, 2]),
                   ymax = max(shpTable[, 3]),
                   zmax = max(shpTable[, 4]),
                   mmin = min(shpTable[, 5]), 
                   mmax = max(shpTable[, 5]))
  }
  shp <- list(shp = shp, header = header)
  shx <- list(index = cbind(Offset = c(50,
                              (cumsum(v.content.length + 4) + 50)[-length(v.content.length)]),
                Length = v.content.length), 
              header = shp$header)
  shx$header$file.length <- 50 + 4 * nrow(shx$index)
  shp.file <- list(shp = shp, shx = shx, dbf = dbf)
  shp.file
}
