Sponsored Content
Top Forums Shell Programming and Scripting Using IF statements with maths where the input is not an integer Post 302546812 by StudentFitz on Friday 12th of August 2011 08:29:21 AM
Old 08-12-2011
Using IF statements with maths where the input is not an integer

Hi All

I've made a few scripts which using GDAL extract the value of a pixel within a given raster. The purpose is to work out the combine value of every pixel. I thought there may have been an easier way to do this but alas!

The code below extracts the pixel value at position X Y. The value is a floating point rather than an integer and thats where my problem lies. The if statement simply exports the value to a txt file. Other scripts take care of the rest ie change values of X Y and summing the resultant txt files.
Code:
#!/bin/sh
input=`gdallocationinfo -valonly 210_1515_volume.env X Y`  # The value of the pixel at coordinate X Y
if [ $input -gt 0 ] ;then
	echo $input > colrow.txt
fi

However, I get the following message back when ever my statement is true because my value isn't an integer;

extract2.sh: line 4: [: 2.15787172317505: integer expression expected

Please note the raster file mostly contains zero values, and its only the values greater than zero I'm interested in, which is why I added the expression.

Any ideas how I can alter this code so the IF statement excepts integers? Alternatively if anyone knows of a faster method of quickly summing the values of a raster file, that would be equally useful.

Many thanks in advance for any assistance you can offer.

Andy

Last edited by vbe; 08-12-2011 at 09:50 AM.. Reason: please use code tags!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with Maths

Heres a script i wrote as a bit of practise. What it does is insert a line in the middle of a file. The line being $1 and the file being $2 #!/bin/bash rm tempfile touch tempfile count=1 linenum= `wc -l < $2` if then echo $1 >> $2 else even=`expr "$linenum" % 2` if then... (3 Replies)
Discussion started by: Quesa
3 Replies

2. Shell Programming and Scripting

isql input file with multiple sql statements

I've got: isql -U $USERID -S $SERVER -D $DATABASE -i inputfile.sql -o outputfile.txt in inputfile I have: go sql#1 go sql#2 go sql#3 go I also tried without "go" and with";" instead which did not work SQL statements will work if I paste them directly into the script and use EOF ... (0 Replies)
Discussion started by: Cailet
0 Replies

3. Shell Programming and Scripting

Perl - maths equation - need help

if input to the perl program is ' ( p * ((a+b) * (c+d))) + q ' it shuld give the output as ' pac + pad + pbc + pbd + q ' .can anyone suggest a way to do this ? (7 Replies)
Discussion started by: Anuj8584
7 Replies

4. Programming

How i can read a long integer from standar input and a string with as many characters as specified..

how i can read a long integer from standar input and a string with as many characters as specified in the number? i thing that i must use the read command ofcourse.... (6 Replies)
Discussion started by: aintour
6 Replies

5. Shell Programming and Scripting

Maths with variables

Hello, I'm trying to write a while loop for a decimal value in tcsh which I know can't be done. Instead I want my increments to be one order of magnitude too large and then divide it by 10 when I use the variable. However, I don't know how to divide my variable and set it as another. set... (1 Reply)
Discussion started by: DFr0st
1 Replies

6. UNIX for Dummies Questions & Answers

Check if input is an integer or a floating point?

Hiii I actually intent to check the integer or floating point number input by user i.e. 23, 100, 55.25, 12.50 ..etc. However, when someone input strings or alpha character, my program has to show invalid input.!! Is there any Unix shell script syntax can help me to check ? Thanking you (2 Replies)
Discussion started by: krishnampkkm
2 Replies

7. Programming

I don't know how to replace input char with appropriate integer

Hi guys, I asked for help on programming forums and no one didn't helped me so I ask for help here. I am playing with some tasks from my book and I can't figure where did I get wrong. From the first program I get a blank screen, program won't generate 10*10 matrix. And second problem is I... (6 Replies)
Discussion started by: solaris_user
6 Replies

8. Shell Programming and Scripting

Simple maths calculator loop.

Hi, I am trying to make a maths calculator that: 1. Prompts the user for a number. 2. Prompts the user for an operation (add, subtract, divide or multiply) 3. Prompts the user for a number. 4. Prompts the user for another operation (same as above) OR the option to get the result for the... (4 Replies)
Discussion started by: johnthebaptist
4 Replies

9. Shell Programming and Scripting

Maths in shell scripts

Hi, Need help on this. I need to increment a variable by 1 but retain as 2 characters. I am using expr to do additions: NEWSERIAL=`expr $SERIAL + 1` $SERIAL can range from 01-99. After adding "1", I need the result to be 2 characters, eg: 02+1 = 03. By default expr will truncate the... (4 Replies)
Discussion started by: vchee
4 Replies

10. Shell Programming and Scripting

Calculate the constant e to 14+ decimal places using integer maths.

Hi guys... I am loving this integer maths thing. 64 bit systems are certainly easier than 32 bit, but hey, I don't intend to leave out my fav' platform. Using one of the 'Brothers' methods, URL inside the code. #!/bin/sh # # #!/usr/local/bin/dash # e_constant.sh # Brother's formula . #... (2 Replies)
Discussion started by: wisecracker
2 Replies
gdallocationinfo(1)					      General Commands Manual					       gdallocationinfo(1)

NAME
gdallocationinfo - gdallocationinfo raster query tool SYNOPSIS
Usage: gdallocationinfo [--help-general] [-xml] [-lifonly] [-valonly] [-b band]* [-l_srs srs_def] [-geoloc] [-wgs84] srcfile [x y] DESCRIPTION
The gdallocationinfo utility provide a mechanism to query information about a pixel given it's location in one of a variety of coordinate systems. Several reporting options are provided. -xml: The output report will be XML formatted for convenient post processing. -lifonly: The only output is filenames production from the LocationInfo request against the database (ie. for identifying impacted file from VRT). -valonly: The only output is the pixel values of the selected pixel on each of the selected bands. -b band: Selects a band to query. Multiple bands can be listed. By default all bands are queried. -l_srs srs def: The coordinate system of the input x, y location. -geoloc: Indicates input x,y points are in the georeferencing system of the image. -wgs84: Indicates input x,y points are WGS84 long, lat. srcfile: The source GDAL raster datasource name. x: X location of target pixel. By default the coordinate system is pixel/line unless -l_srs, -wgs84 or -geoloc supplied. y: Y location of target pixel. By default the coordinate system is pixel/line unless -l_srs, -wgs84 or -geoloc supplied. This utility is intended to provide a variety of information about a pixel. Currently it reports three things: o The location of the pixel in pixel/line space. o The result of a LocationInfo metadata query against the datasource - currently this is only implemented for VRT files which will report the file(s) used to satisfy requests for that pixel. o The raster pixel value of that pixel for all or a subset of the bands. o The unscaled pixel value if a Scale and/or Offset apply to the band. The pixel selected is requested by x/y coordinate on the commandline, or read from stdin. More than one coordinate pair can be supplied when reading coordinatesis from stdin. By default pixel/line coordinates are expected. However with use of the -geoloc, -wgs84, or -l_srs switches it is possible to specify the location in other coordinate systems. The default report is in a human readable text format. It is possible to instead request xml output with the -xml switch. For scripting purposes, the -valonly and -lifonly switches are provided to restrict output to the actual pixel values, or the LocationInfo files identified for the pixel. It is anticipated that additional reporting capabilities will be added to gdallocationinfo in the future. EXAMPLE
Simple example reporting on pixel (256,256) on the file utm.tif. $ gdallocationinfo utm.tif 256 256 Report: Location: (256P,256L) Band 1: Value: 115 Query a VRT file providing the location in WGS84, and getting the result in xml. $ gdallocationinfo -xml -wgs84 utm.vrt -117.5 33.75 <Report pixel="217" line="282"> <BandReport band="1"> <LocationInfo> <File>utm.tif</File> </LocationInfo> <Value>16</Value> </BandReport> </Report> AUTHORS
Frank Warmerdam warmerdam@pobox.com GDAL
Tue Sep 18 2012 gdallocationinfo(1)
All times are GMT -4. The time now is 12:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy