1

I would like to eventually create a shapefile in ArcGIS and then extract it as a matrix to be read by a spatial package I use in R. By matrix I actually mean a raster file.

My first step is to create a simple binary matrix of a map (UK for example) with 1 - water and 2- land. As a newcomer to both coding and GIS, I am struggling to come up with a solution. - converting to raster and exporting as text file was exactly what I needed.

I have been looking into spatial weights matrices - does this sound like the right way forward?

This will eventually need to be executed multiple times with different layers and different data sets - so will need a Python script for it. Right now I am just happy with exporting one text file for one layer.

Current problem: Although I can change the cell size - I really need to be able to alter the number of columns and rows. I want it to be a 300 x 300 matrix to save any complicated maths later on.

Background: The package I use (not yet on CRAN) looks at spatial points and displays them on Google Maps with predicted 'sources'. I would like to develop this and reducing the search area (give more informed priors). For example 'don't search in water'. To do this I need a nice matrix from ArcGIS.

Any ideas?

4
  • 1
    Assuming that you mean "raster" by "matrix", it sounds like you just want to use "Polygon to Raster" (which requires Spatial Analyst or an Advanced license of ArcGIS Desktop). Please edit the question to explain what have you produced so far, and whether you need to execute this once (using the UI) or want to script it in Python.
    – Vince
    Commented Aug 3, 2015 at 10:56
  • 1
    It sounds like you should try to do everything in R. Does the package use a SpatialPointsDateFrame as input?
    – Simbamangu
    Commented Aug 3, 2015 at 11:16
  • Many thanks for everything - sorry for delayed response or ill informed post (first time posting!). Converting to Raster seemed have have made the step I wanted to get. I was able to export the raster as a text file and am now making progress to get my R package to read it.
    – Sally777
    Commented Aug 7, 2015 at 10:23
  • Welcome to GIS SE Sally. After reading your comment, it sounds like you have found a solution to your question. If so, could you please provide it as an answer? Otherwise, it would be helpful to clarify your question to help readers understand what exactly you need help with.
    – Aaron
    Commented Aug 7, 2015 at 11:08

2 Answers 2

2

Thanks everyone.

Converting the layer to a raster and exporting as a text file was exactly what I needed for the time being. I used the convert to Raster function in the toolbox. .

You can then use the raster to ASCII function to save the data as a txt file which can then be read by my r package.

2
  • I would recommend adding more details to this answer so that it will be helpful to future readers. Also, this is not the correct area for asking follow-up questions--for that, it is best to edit your original question to clarify, or to open a new question.
    – Aaron
    Commented Aug 10, 2015 at 11:42
  • 1
    Aaron thats for your time in replying - I am not sure what more you want me to add let me know if it still is not ok I am happy to update. I was asking for a binary matrix which actually is simply a raster file. Being so new to this I hadn't realised.
    – Sally777
    Commented Aug 10, 2015 at 13:51
0

You are over complicating this a bit. In ArcGIS you can save your raster(s) as tiff or img formats. In R you can use the raster package to read the rasters (using raster(), stack() or brick() functions) and covert to a matrix or array in a single step. If you have a single raster then you would use the raster() function. However, if you are working with a stack (multi band) raster object then you would use the stack() or brick() functions and coerce using as.array().

library(raster)

#Create an example raster, would result in same object as reading raster from disk
r <- raster(matrix(rnorm(25),5))

# Coerce raster to matrix
m <- as.matrix(r)

# In a single step
( m <- as.matrix(raster(matrix(rnorm(25),5))) )

# Here is an array example
r <- stack(raster(matrix(rnorm(25),5)), raster(matrix(rnorm(25),5)))
( a <- as.array(r) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.