R/export.R

Defines functions getShapeGraphLinksUnlinks getShapeGraphConnections getShapeGraph processPointMapAndLinks processPointMap getPointmapDataAndLinks getPointmapLinks getPointmapData export

Documented in export getPointmapData getPointmapDataAndLinks getPointmapLinks getShapeGraph getShapeGraphConnections getShapeGraphLinksUnlinks processPointMap processPointMapAndLinks

# Copyright 2019 Fani Kostourou
# Copyright 2019 Petros Koutsolampros
#
# This file is part of rdepthmap
#
# rdepthmap is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# rdepthmap is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with rdepthmap  If not, see <https://www.gnu.org/licenses/>.

export = function(graphFileIn, fileOut, exportType,
                  cliPath = getDefaultCLILocation(), verbose = FALSE) {
  depthmapXcli(c("-f", formatForCLI(graphFileIn),
                 "-o", formatForCLI(fileOut),
                 "-m", "EXPORT",
                 "-em", exportType), cliPath, verbose);
}

getPointmapData = function(graphFileIn, scale = 1,
                           cliPath = getDefaultCLILocation(), verbose = FALSE) {
  mapFile = tempfile(fileext = ".csv");
  rdepthmap::export(graphFileIn, mapFile, "pointmap-data-csv", cliPath, verbose)
  dpm = processPointMap(mapFile, scale, ",")
  file.remove(mapFile)
  return(dpm);
}

getPointmapLinks = function(graphFileIn,
                            cliPath = getDefaultCLILocation(), verbose = FALSE) {
  csvFile = tempfile(fileext = ".csv");
  rdepthmap::export(graphFileIn, csvFile, "pointmap-links-csv", cliPath, verbose)
  links = read.csv(csvFile)
  file.remove(csvFile)
  return(links);
}


getPointmapDataAndLinks = function(graphFileIn, scale = 1,
                                   cliPath = getDefaultCLILocation(), verbose = FALSE) {
  mapFile = tempfile(fileext = ".csv");
  rdepthmap::export(graphFileIn, mapFile, "pointmap-data-csv")
  linkFile = tempfile(fileext = ".csv");
  rdepthmap::export(graphFileIn, linkFile, "pointmap-links-csv")
  dpm = processPointMapAndLinks(mapFile, linkFile, scale, ",")
  file.remove(mapFile)
  file.remove(linkFile)
  return(dpm);
}

processPointMap = function(mapPath, scale = 1, sep = "\t") {
  pointMapData = read.csv(mapPath, sep = sep);
  pointMapData$x = pointMapData$x*scale;
  pointMapData$y = pointMapData$y*scale;
  pointMapData = cbind(pointMapData, refIDtoIndex(pointMapData$Ref));
  xycols = c("x","y");
  pointMapPoints = pointMapData[, xycols];
  pointMapData = pointMapData[,!(names(pointMapData) %in% xycols)];
  dpm = SpatialPointsDataFrame(pointMapPoints, data = pointMapData);
  makeGridded = function() {gridded(dpm) <<- TRUE}
  suppressWarnings(makeGridded())
  return(list(map = dpm, importScale = scale));
}

processPointMapAndLinks = function(mapPath, linkPath = NA, scale = 1, sep = "\t") {
  pointMap = processPointMap(mapPath, scale, sep)
  pointMap$links = NA
  if (!is.na(linkPath)) {
    pointMap$links = read.csv(linkPath, sep = sep)
  }
  return(pointMap);
}

getShapeGraph = function(graphFileIn,
                         cliPath = getDefaultCLILocation(), verbose = FALSE) {
  mapFile = tempfile(fileext = ".mif")
  rdepthmap::export(graphFileIn, mapFile, "shapegraph-map-mif", cliPath, verbose)
  ogr = sf::st_read(mapFile, geometry_column = 1L, quiet = !verbose)
  file.remove(mapFile)
  return(ogr);
}

getShapeGraphConnections = function(graphFileIn,
                         cliPath = getDefaultCLILocation(), verbose = FALSE) {
  connectionsFile = tempfile(fileext = ".csv")
  rdepthmap::export(graphFileIn, connectionsFile, "shapegraph-connections-csv", cliPath, verbose)
  csv = read.table(connectionsFile,header = TRUE, sep = ",")
  file.remove(connectionsFile)
  return(csv);
}

getShapeGraphLinksUnlinks = function(graphFileIn,
                                    cliPath = getDefaultCLILocation(), verbose = FALSE) {
  linksunlinksFile = tempfile(fileext = ".csv")
  rdepthmap::export(graphFileIn, linksunlinksFile, "shapegraph-links-unlinks-csv", cliPath, verbose)
  csv = read.table(linksunlinksFile,header = TRUE, sep = ",")
  file.remove(linksunlinksFile)
  return(csv);
}
orange-vertex/rdepthmap documentation built on Oct. 26, 2023, 7:40 a.m.