2009-06-01

Add a column to a text file from a raster

Text files are great

I have mixed feelings about software interoperability. Sure GRASS interfaces with R, and R can interface with Excel and Access if you have the ODBC drivers. But, often users do not have the energy to learn the best techniques for moving data back and forth between programs. Further, there always seems to be some quirk with data that prevents the transfer from going as smoothly as the user's manual examples do. Then, I remember the lowly text file. Whether you use comma separated values (csv) files or tab-delimited files, every software platform on the planet can read text files of records (lines) and fields (values on a line separated by commas,tabs or some other character). If you're trying to send data to someone else, you can be confident that your recipient can read them. It may not be the flashiest or fastest way to move data between people and systems, but it always works. Finally, when you use plain text files for data, you have many more options for working with the files. Tools such as bash, awk, sed, perl, and any other programming language you can think of will can be brought to bear on data analysis problems.

Attaching raster data to points in GIS

Switch gears for a moment from a common file format to a common analysis task. For researchers who work with spatial point patterns (like plots), a regular step in data analysis is to pull data from raster layers where they intersect the point data and add it as columns to the point data. Informally, these are known as "drill-down" operations. You have a stack of maps and you want to drill down through them vertically at point locations, grab the data, and add it to the points. This is an a relatively simple task in ArcGIS if you use Hawth's tools. It's also pretty easy in GRASS, though it involves a couple of steps. The function v.what.rast will put raster values into a column of a point dataset. The column must already exist in the dataset, which can be done through the command v.db.addcol and v.what.rast function just fills in the missing values with values where the raster intersects the points.

Adding raster values to text files

Now, back to text files. I recently worked with a graduate student who wanted elevation values from a lidar-derived digital elevation model (dem) on 4 samples totalling about 1100 plots. She is not a GRASS user, and so she sent me the locations of all the plots along with an identifier in csv format. I wanted to send her back the same csv files with elevation simply added as a field. I wrote a bash script to accomplish this quickly and repeatably for her files. In this script, all the GRASS functions are hidden from the user. Users simply input a csv file and the raster column you wish to add, and a csv file is output with that column added. The script requires the awk script that I posted previously, which recognizes the types of varibles present in the input csv. I have included a portion of the full script below. GRASS has a very particaular code style that it uses for running scripts natively (See g.parser for details). This makes the script very long. So, I've cut out the not-so-important parts. If you would like the full file you can get it here.

v.ascii.addrastcol

#!/bin/sh
##################################################################
#
# MODULE:       v.ascii.addrastcol
# AUTHOR(S):    R. Todd Jobe 
# PURPOSE:      Add a raster value field to a csv of points
# COPYRIGHT:    (C) 2009 by R. Todd Jobe
#
#  This program 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 2 of the License, or
#  (at your option) any later version.
#
#  This program 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.
#
##################################################################
# . . . Grass header information removed
# . . . The cut code creates variables GIS_OPT_* and GIS_FLAG_*
# . . . Get the full code at:
# . . . http://www.unc.edu/~toddjobe/src/v.ascii.addrastcol
##################################################################
# Need space-delimited arguments for looping
GIS_OPT_INPUT=${GIS_OPT_INPUT//,/ }

# test for input raster map
eval `g.findfile element=cell file="$GIS_OPT_RAST"`
if [ ! "$file" ] ; then
   g.message -e "Raster map <$GIS_OPT_RAST> not found in mapset search path"
   exit 1
fi

for i in $GIS_OPT_INPUT; do
   # Check that input files exist
   if [ ! "$i" ] ; then
 echo "file $i does not exist" >&2
 exit $E_NOTFOUND
   fi

   # Create header line if necessary and get variable types
   if [ $GIS_FLAG_H -eq 1 ]; then
 skip=1
   else
 skip=0
   fi

   # Intermediate variables
   map=`basename $i .csv`
   tmp=`tempfile -s ".csv"`
   of="${map}${GIS_OPT_SUFFIX}"
   col=`typeVars.awk -v h=$GIS_FLAG_H -F, $i`

   # Get the raster type
   if `r.info -t $GIS_OPT_RAST | grep -q "FCELL"`; then
 rtype="double precision"
   else
 rtype="int"
   fi
  
   # Grass-specific calls
   v.in.ascii --overwrite input="$i" output="$map" fs="$GIS_OPT_FS" \
 skip=$skip column="$col" x="$GIS_OPT_X" y="$GIS_OPT_Y"
   v.db.addcol map="$map" \
 columns="$GIS_OPT_RAST $rtype"
   v.what.rast vector="$map" column="$GIS_OPT_RAST" \
 raster="$GIS_OPT_RAST"
   v.out.ascii input="$map" fs="," \
 columns="$GIS_OPT_RAST" | \
   awk -F, -v meters="$GIS_FLAG_M" -v head="$GIS_FLAG_H" \
 -v rast="$GIS_OPT_RAST" '
   BEGIN{ if(head == 1) print rast;}
   {
        # Convert Ft. elevation to meters if necessary
        if($4!="" && meters==1) $4=$4 * 0.30480060
        print $4
   }' >"$tmp"
   # Join raster values to input and write to output
   paste -d$GIS_OPT_FS "$i" "$tmp" >"$of"

   # Cleanup
   rm "$tmp"
   if [ $GIS_FLAG_R -eq 1 ] ; then
 g.remove -f --quiet vect=$map
   fi
done
To see the useage for the full script, you can simply start GRASS and from the command line type v.ascii.addrastcol --help. Here's a short example for a random set of 25 points on Ft. Bragg North Carolina.
GRASS 6.4.0RC3 (ftBragg):~ > head locs.csv
"","V1","V2","V3"
"1",594927.510605283,153893.107849400,1
"2",590884.780751329,155855.275043186,2
"3",594095.580107841,155284.234116311,3
"4",605646.865006463,154397.274762958,4
"5",603002.815879479,153284.801461238,5
"6",603453.085431226,152633.248500899,6
"7",602981.046299442,152156.288326646,7
"8",604160.50828444,151921.617483228,8
"9",603752.189821169,152062.642227756,9
GRASS 6.4.0RC3 (ftBragg):~ > v.ascii.addrastcol --quiet -hr \
input=locs.csv rast=iLandcover x=2 y=3 --quiet -r
GRASS 6.4.0RC3 (ftBragg):~ > head locs_isect.csv
"","V1","V2","V3",iLandcover
"1",594927.510605283,153893.107849400,1,3
"2",590884.780751329,155855.275043186,2,3
"3",594095.580107841,155284.234116311,3,1
"4",605646.865006463,154397.274762958,4,3
"5",603002.815879479,153284.801461238,5,3
"6",603453.085431226,152633.248500899,6,3
"7",602981.046299442,152156.288326646,7,3
"8",604160.50828444,151921.617483228,8,1
"9",603752.189821169,152062.642227756,9,3

Labels: , , ,

235 Comments:

«Oldest ‹Older 201 – 235 of 235
Blogger rrb said...

https://www.12thmodelquestionpaper.in/
https://www.10thmodelquestionpaper.in/


Download 10th 12th class model questions paper 2020

January 9, 2019 at 12:46 AM  
Blogger Praylin S said...

Awesome post with great piece of information. Thanks for sharing. I really liked it.

Oracle Training in Chennai
Oracle Training institute in chennai
Unix Training in Chennai
Unix Shell Scripting Training in Chennai
LINUX Training in Chennai
LINUX Course in Chennai
Oracle Training in Velachery
Oracle Training in Tambaram

January 10, 2019 at 5:26 AM  
Blogger jefrin said...

Very good to read the post
best ccna training in chennai

January 18, 2019 at 6:09 AM  
Blogger Saro said...

I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.
rpa training in bangalore
rpa training in chennai
rpa training in pune
best rpa training in bangalore

January 22, 2019 at 2:02 AM  
Blogger kevin antony said...

I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.
rpa training in bangalore
rpa training in chennai
rpa training in pune
best rpa training in bangalore

January 22, 2019 at 2:04 AM  
Blogger rohini said...

I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
iWatch service center chennai | apple ipad service center in chennai | apple iphone service center in chennai

January 28, 2019 at 3:35 AM  
Blogger Jaweed Khan said...

Thanks For Sharing Your Information Please Keep UpDating Us The Information Shared Is Very Valuable Time Went On Just Reading The Article Python Online Training Devops Online Training
Aws Online Training DataScience Online Training
Hadoop Online Training

February 21, 2019 at 7:12 AM  
Blogger Joe said...

Wonderful Post. Brilliant piece of work. It showcases your in-depth knowledge. Thanks for Sharing.
Ionic Training in Chennai
Ionic Course in Chennai
Ionic Training Course
Ionic Framework Training
Ionic Training near me
Ionic Training in Velachery
Ionic Training in Tambaram

March 28, 2019 at 5:24 AM  
Blogger suresh said...

This is Very Useful blog, Thank you to Share this.
Salesforce Training in Chennai

Microsoft Azure Training in Chennai

Openstack Training in Chennai


April 1, 2019 at 11:12 AM  
Blogger VenuBharath2010@gmail.com said...

Awesome Post. Great Content. It is very inspiring to read your post. Waiting for your future updates.
IoT courses in Chennai
IoT Courses
IoT Training
IoT certification
Internet of Things Training in Chennai
IoT Training in Tambaram
IoT Training in OMR

April 4, 2019 at 6:11 AM  
Blogger Aruna Ram said...

This blog is awesome! In this post is very interesting for all readers and I am waiting for your more post from this blog admin.

Embedded System Course Chennai
Embedded Course in chennai
Power BI Training in Chennai
Tableau Training in Chennai
Pega Training in Chennai
Excel Training in Chennai
Corporate Training in Chennai
Embedded System Course Chennai
Embedded Training in Chennai

May 4, 2019 at 1:42 AM  
Blogger Rajesh said...

thanks for sharing this information
python training in bangalore marathahalli
python training institutes in bangalore marathahalli
python training in btm Layout
Artificial Intelligence training in BTM
data science with python training in BTM
Machine Learning training in btm
Qlik Sense Training in BTM

June 20, 2019 at 3:26 AM  
Blogger Mithun said...

Great Post with lots of useful information. Special thanks for this blog highly useful..
SAP Training in Chennai | SAP ABAP Training in Chennai | SAP FICO Training in Chennai | SAP MM Training in Chennai

August 5, 2019 at 4:38 AM  
Blogger Big Data Hadoop training institutes said...

Really Interesting Article . Nice information will look forwardr next article update Keep posting the articles :)
If you are looking for any data science Related information please visit our website Data Science Course In Bangalore page!

September 17, 2019 at 8:14 AM  
Blogger drivetrain said...

Choose high quality and durable dennys driveshaft replacement parts for your Nissan. Replacement parts are available for your air intake system, body electrical, body mechanical and trim, body sheet metal, brakes, climate control, clutch, cooling system, diesel injection, drive belts, drive shafts and axle, engine electrical, engine parts, exhaust, fuel delivery, steering, suspension, tools and hardware, transmission. Replacement parts keep your Nissan running and looking great, these parts will surely make it more stylish, more fun to drive, more comfortable and convenient, and more high-tech. dennys driveshaft .

November 18, 2019 at 11:15 AM  
Blogger raju said...

nice post....!
inplant training in chennai
inplant training in chennai for it.php
panama web hosting
syria hosting
services hosting
afghanistan shared web hosting
andorra web hosting
belarus web hosting
brunei darussalam hosting
inplant training in chennai

December 16, 2019 at 2:15 AM  
Blogger dras said...

very nice post..
inplant training in chennai
inplant training in chennai
inplant training in chennai for it
Australia hosting
mexico web hosting
moldova web hosting
albania web hosting
andorra hosting
australia web hosting
denmark web hosting

December 17, 2019 at 5:51 AM  
Blogger vijay said...

192.168.0.1
192.168.1.1 popular ip address

February 26, 2020 at 5:56 AM  
Blogger Healthcare mailing said...


Check out Healthcare Mailing Nurses Email List

Doctors Email List

physicians Email List

March 6, 2020 at 9:56 AM  
Blogger svrtechnologies said...

Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..

sap abap tutorial

March 11, 2020 at 4:47 AM  
Blogger Rashika said...

I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post.
Hats off to you! The information that you have provided is very helpful.
AWS training in chennai | AWS training in anna nagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

May 21, 2020 at 12:44 PM  
Blogger varsha said...


It would have been the happiest moment for you,I mean if we have been waiting for something to happen and when it happens we forgot all hardwork and wait for getting that happened.
AWS training in chennai | AWS training in annanagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

June 2, 2020 at 10:19 AM  
Blogger brtech said...

Amazing post, thanks for sharing.

October 11, 2020 at 1:20 AM  
Blogger vijay said...



router-networks.com

192.168.o.1 1

192.168.100 1

10.0.0.0 1

192.168.l.1

October 24, 2020 at 2:17 AM  
Blogger Winspira said...

https://hangthebankers.com/how-to-pay-off-your-personal-debts/

February 15, 2021 at 11:10 AM  
Blogger Subhasree said...

Excellent post! Your post is very useful and I felt quite interesting reading it. Expecting more post like this. Thanks for posting such a good post. laptop service in home. To service your laptop with offer prices, Please visit : Laptop service center in Navalur

February 28, 2021 at 9:53 AM  
Blogger Prwatech said...

Thanks for sharing information to our knowledge, it helps me plenty keep sharing…

Best Tableau Training Institute in Pune
Apache Spark Training Institute in Pune

March 23, 2021 at 8:23 AM  
Blogger Ritesh Singh said...

Visit Bharat Go Digital Academy to learn the digital marketing skills in India.

May 15, 2021 at 3:01 PM  
Blogger Unknown said...

The AWS certification course has become the need of the hour for freshers, IT professionals, or young entrepreneurs. AWS is one of the largest global cloud platforms that aids in hosting and managing company services on the internet. It was conceived in the year 2006 to service the clients in the best way possible by offering customized IT infrastructure. Due to its robustness, Digital Nest added AWS training in Hyderabad under the umbrella of other courses.

September 4, 2021 at 2:45 AM  
Blogger Ramesh Sampangi said...

Really Fantastic blog, awesome information and knowledgeable content. Keep sharing more blogs. Thanks for sharing this blog with us.
AI Patasala Data Science Training

December 20, 2021 at 5:36 AM  
Blogger vcube said...

Nice blog with useful information. I'm really amazed by your blog. Continue your good effort in future blogs.
Learn Best Python Fullstack Course In Hyderabad

October 6, 2023 at 1:27 AM  
Blogger vcube said...

Interesting post! I appreciate you sharing this very helpful information. In order to continue blogging and expand my skill set, I would like to read your next post.
Best DevOps training institute in hyderabad

December 25, 2023 at 4:46 AM  
Blogger Croma Campus said...

Thank you for sharing your knowledge and expertise with the community. Start on a  AWS journey.

March 8, 2024 at 5:19 AM  
Blogger Starknovablog said...

If you're aiming to enhance your database management skills and achieve Oracle certification, I strongly suggest exploring the Oracle training in Chennai provided by IntelliMindz. Their detailed training modules and expert trainers ensure you get hands-on experience and in-depth knowledge. For more information, visit their Oracle training in Chennai page.

Oracle Training in Chennai

June 10, 2024 at 12:26 AM  
Blogger kosmik said...

Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
sql-server-training-in-hyderabad

August 7, 2024 at 3:42 AM  
«Oldest ‹Older 201 – 235 of 235

Post a Comment

Subscribe to Post Comments [Atom]

<< Home