Showing posts with label Help Guide. Show all posts
Showing posts with label Help Guide. Show all posts

Wednesday, March 04, 2020

How to remove columns in SpatialPolygonsDataFrame in R?

If you have a spatial polygon data frame (SPDF) and you would want to remove some columns, how do you do it?  You can use R to remove columns in SpatialPolygonsDataFrame .

Use the syntax object_ name[,-(1:10)] to remove columns 1 to 10 or object_name[,-c(1,10)] to drop columns 1 and 10.

require(maptools)

#load shapefile from maptools package
 
w <- readShapeSpatial(system.file("shapes/sample.shp", package="maptools")[1],
                   IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))

class(xx) #check the object class
 
#[1] "SpatialPolygonsDataFrame"
#attr(,"package")
#[1] "sp"
#remove columns 1 to 10 
ss <- w[,-(1:10)]
head(ss@data) 
#or use column names to remove them 
rem <- c("name1","name2") # list of column names
final <- rem[,!(names(rem) %in% drops)] #remove columns "name1" and "name2"
 

Thursday, February 27, 2020

How to Add Pie Charts on Top of GIS Maps Using R Package

Question: How do you add pie charts on top of GIS shapefile maps using an R code or package? 

Below I list a few of the R code options depending on the package you want to use.

1. mapplots package

Create simple maps; add sub-plots like pie plots to a map or any other plot; format, plot and export gridded data. The package was developed for displaying fisheries data but most functions can be used for more generic data visualization.  Check out this example.


2. rworldmap package

Enables mapping of country level and gridded user datasets.  Check out this example.

3. plotGoogleMaps package 

Plot htm output with Google Maps API and a plot of spatial data as a combination of users' data and Google Maps layers. Ready to use as local htm file or into your own website.  Check out this tutorial.


Wednesday, February 19, 2020

R Code to process vector to raster data fast

Use this R Code to convert and process vector to raster data fast.

Option 1 
# Load libraries
library('raster')
library('rgdal')
library('parallel') 

# Load a SpatialPolygonsDataFrame 
# Load shapefile
Data_sample <- system.file("external/lux.shp")
Data_sample2 <- shapefile(Data_sample) 

# Define RasterLayer object
r.raster <- raster()

# Define raster extent
extent(r.raster) <- extent(Data_sample2)

# Define pixel size
res(r.raster) <- 0.1
# Rasterize
system.time(Data_sample2.r <- rasterize(Data_sample2, r.raster)) 
Option 2
# Calculate the number of cores
num_cores <- detectCores() - 1

# Number of polygons features
features <- 1:nrow(Data_sample2[,])

# Split features in n parts
n <- 20
parts <- split(features, cut(features, n))

# Initiate cluster (after loading all the necessary object to R environment: Data_sample2, parts, r.raster, n)
cl <- makeCluster(num_cores, type = "FORK")
print(cl)

# Parallelize rasterize function
system.time(rParts <- parLapply(cl = cl, X = 1:n, fun = function(x) rasterize(Data_sample2[parts[[x]],], r.raster)))

# Finish
stopCluster(cl)

# Merge all raster parts and plot
Merge_parts <- do.call(merge, rParts)
plot(Merge_parts)

Friday, July 15, 2016

Species Distribution Model SDM Package Tool in R Software

I used the Species Distribution Model SDM Package Tool in R software all the time.

sdm is an object-oriented, reproducible and extensible R platform for species distribution modelling. The sdm package is designed to create a comprehensive modelling and simulation framework that: 

1) provides a standardised and unified structure for handling species distributions data and modelling techniques (e.g. a unified interface is used to fit different models offered by different packages);

2) is able to support markedly different modelling approaches;

3) enables scientists to modify the existing methods, extend the framework by developing new methods or procedures, and share them to be reproduced by the other scientists;

4) handles spatial as well as temporal data for single or multiple species;

5) employs high performance computing solutions to speed up modelling and simulations, and finally;

6) uses flexible and easy-to-use GUI interface. For more information, check the published paper by Naimi and Araujo (2016) in the journal of Ecography.

Download the package here.

Thursday, August 04, 2011

Free Bootstrap Statistics Software - Trial Version

I was looking for a quick solution to doing Bootstrap or Bootstrapping statistics without writing a code. In times like this, I usually turn to R codes. In some lazy days, I use Excel macro. But now, I was determined to use a free software (at least a free trial) that does Bootstrapping stats.

My goal was to do bootstrap regression in my limited 20 samples and estimate properties of the beta coefficients.

I found one software that does it flawlessly - the S-plus. It is now own by TIBCO Software Inc (I thought it was owned by Insightful Corporation). You can download the free S-plus trial software here. If you download the S-plus, you get the chance to try the TIBCO Spotfire as well. Spotfire plots data with interactive visualization!

If you do not want a trial version, and prefer to use Microsoft Excel instead to do your Bootstrap, there is also an easy way to do it. Use the Excel Bootstrap add-in. The add-in enables bootstrapping from within Excel environment. Download and follow the instructions to install the plug-in.

For Bootstrapping Help, I found this guide (pdf) to help you interpret Bootstrap results.

Visit also the "Resample Library User’s Manual" for any of your the S-plus Bootstrap questions.

Friday, July 10, 2009

ENVI Band Math Tutorial, Help Guide

Looking for the ENVI Band Math tutorial or help guide? Look no more. I found a useful ENVI Band Math tutorial document from ENVI's users guide.

ENVI Band Math is a flexible image processing tool with many capabilities not available in any other image processing system. You can use ENVI's Band Math dialog to define bands or files used as input, to call a user Band Math function, and to write the result to a file or memory. ENVI's Band Math function accesses data spatially by mapping variables to bands or files. Spatial data that are too large to read entirely into memory are automatically accessed using ENVI's data tiling.

I found the document while searching for the right Band Math expression for the different data types and their casting functions. Here are few of them: data type, casting function, dynamic range.

Byte: byte() : - 0 to 255

Integer: fix() : -32768 to + 32767

Unsigned integer: uint() : 0 to 65535

Long integer: long() : approximately +/– 2 billion

Unsigned long integer: ulong() : 0 to approximately 4 billion

Here is the complete Band Math Function Tutorial

Thursday, July 09, 2009

Copy Excel data, chart to Word or PowerPoint

There are always easy things we think we already know, yet we actually don't. There are also easy things that we might have used or tried before but have forgotten them already. Such as the case of a friend who asked me how to copy Excel data and charts to Word or PowerPoint, without importing the entire workbook into your Word document or PowerPoint slide.

Here are the easy steps.

1. Select the data or chart you want to copy.
2. Click Copy Button image.
3. Switch to Microsoft Word or Microsoft PowerPoint.
4. Click in the document or presentation where you want to put the Microsoft Excel data or chart, and do one of the following:

How to Copy and Paste the Excel data into Word
1. Click Paste on the Formatting toolbar (toolbar: A bar with buttons and options that you use to carry out commands. To display a toolbar, click Customize on the Tools menu, and then click the Toolbars tab.).

2. Click Paste Options next to the data, and then do one of the following:

- To paste the data as a Word table, click Match Destination Table Style or Keep Source Formatting.

- To paste a link to the Excel data, so that the data in the Word document is updated whenever you change the data in the original Excel workbook, click Match Destination Table Style and Link to Excel or Keep Source Formatting and Link to Excel.

- To paste the data as text with each row in a separate paragraph and tabs separating the cell values, click Keep Text Only.

How to Copy and Paste an Excel chart into Word
1. Click Paste on the Formatting toolbar (toolbar: A bar with buttons and options that you use to carry out commands. To display a toolbar, click Customize on the Tools menu, and then click the Toolbars tab.).

2. Click Paste Options next to the chart, and then do one of the following:

- To paste the chart as a chart, so that when you double-click the chart in the Word document you can use Excel to edit it, click Excel Chart.

- To paste a link to the Excel chart, so that the chart in the Word document is updated whenever you change the chart in the original Excel workbook, click Link to Excel Chart.

- To paste a bitmap picture of the chart, click Picture of Chart.

How to Copy and Paste the Excel data into PowerPoint
* On the Edit menu, click Paste Special, and then do one of the following:

- To paste the data as an Excel range, so that when you double-click the range in the presentation you can edit it with Excel, click Microsoft Excel Worksheet Object. If data is cut off, double-click the object and resize the columns to show all the data.

- To paste a link to the Excel data, so that the data in the presentation is updated whenever you change the data in the original Excel workbook, click Paste link, and then click Microsoft Excel Worksheet Object.

- To paste the data as text that you can edit in PowerPoint, click Formatted Text (RTF) or Unformatted Text.

- To paste a bitmap picture of the data, click Picture or Bitmap.

How to Copy and Paste an Excel chart into PowerPoint
* On the Edit menu, click Paste Special, and then do one of the following:

- To paste the chart as a chart, so that when you double-click the chart in the presentation you can use Excel to edit it, click Microsoft Excel Chart Object.

- To paste a link to the Excel chart, so that the chart in the presentation is updated whenever you change the chart in the original Excel workbook, click Paste link, and then click Microsoft Excel Chart Object.

- To paste a bitmap picture of the chart, click Picture.