473,626 Members | 3,325 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Line algorithim for python and numeric

Hi everyone,

I'm wondering if anyone knows of a way to extract data from a numeric
array along a line. I have a gridded dataset which I would like to be
able to choose two points and extract a 1-d array of the data values
along the line between the two points. Any ideas?

Thanks,
Brian

Oct 13 '06 #1
8 1833
On 13 Oct 2006 07:33:17 -0700, bc*******@gmail .com <bc*******@gmai l.comwrote:
Hi everyone,

I'm wondering if anyone knows of a way to extract data from a numeric
array along a line. I have a gridded dataset which I would like to be
able to choose two points and extract a 1-d array of the data values
along the line between the two points. Any ideas?
Are these all 45 degree lines or what?

If not, you'll have to use trigonometry and approximate the closest
matching cell. (Don't worry, Python has maths functions!! :))

-- Theerasak
Oct 13 '06 #2
> I'm wondering if anyone knows of a way to extract data from a numeric
>array along a line. I have a gridded dataset which I would like to be
able to choose two points and extract a 1-d array of the data values
along the line between the two points. Any ideas?

Are these all 45 degree lines or what?

If not, you'll have to use trigonometry and approximate the closest
matching cell. (Don't worry, Python has maths functions!! :))

There are a couple of optimal/easy cases where the run is
horizontal, vertical, or as you describe, 45-degrees (where
x_step = y_step or x_step = -y_step)

Any other set of arbitrary points, and you will have to specify
the behavior a little more finely.

You can use something like Bresenham's algorithm to march either
"over" or "up and over" to just pick out the "one point at each
step that falls closest to the actual line" along the run.
There's also the Wu Anti-aliasing line algorithm which takes
something akin to Bresenham's algorithm and then samples the
potential points to yield an "anti-aliased" result which averages
the two potential data-points (traditionally colors, but they
could really be any average-able data values).

I'm not sure I've seen either of them implemented in Python
before (though actually *looking* for them might increase those
odds ;-)

http://en.wikipedia.or g/wiki/Xiaolin_Wu's_li ne_algorithm

has details and a pseudo-code implementation (which shouldn't be
too far from the Python code). It's also got links to stuff on
Bresenham's.

-tkc
Oct 13 '06 #3
"Theerasak Photha" <ha********@gma il.comwrote in message
news:ma******** *************** *************** @python.org...
On 13 Oct 2006 07:33:17 -0700, bc*******@gmail .com <bc*******@gmai l.com>
wrote:
>Hi everyone,

I'm wondering if anyone knows of a way to extract data from a numeric
array along a line. I have a gridded dataset which I would like to be
able to choose two points and extract a 1-d array of the data values
along the line between the two points. Any ideas?

Are these all 45 degree lines or what?

If not, you'll have to use trigonometry and approximate the closest
matching cell. (Don't worry, Python has maths functions!! :))

-- Theerasak
No need for that messy trig stuff - simpler and faster would be to use
linear interpolation, since you know the starting and ending cell
coordinates, and how many values you want, compute the array of... oh, hell,
here it is:

start = (4,10)
end = (304,310)
n = 11

dn = ((end[0]-start[0])/float(n),(end[1]-start[1])/float(n))
interpedNodes = [ (start[0]+dn[0]*x, start[1]+dn[1]*x) for x in range(n+1) ]

for nod in interpedNodes:
print nod
print

cellNodes = map(lambda ii: (int(round(ii[0])), int(round(ii[1]))),
interpedNodes)
for nod in cellNodes:
print nod

Prints out:
(4.0, 10.0)
(31.27272727272 7273, 37.272727272727 273)
(58.54545454545 4547, 64.545454545454 547)
(85.81818181818 1813, 91.818181818181 813)
(113.0909090909 0909, 119.09090909090 909)
(140.3636363636 3637, 146.36363636363 637)
(167.6363636363 6363, 173.63636363636 363)
(194.9090909090 9091, 200.90909090909 091)
(222.1818181818 1819, 228.18181818181 819)
(249.4545454545 4547, 255.45454545454 547)
(276.7272727272 7275, 282.72727272727 275)
(304.0, 310.0)

(4, 10)
(31, 37)
(59, 65)
(86, 92)
(113, 119)
(140, 146)
(168, 174)
(195, 201)
(222, 228)
(249, 255)
(277, 283)
(304, 310)
Oct 13 '06 #4
"Tim Chase" <py*********@ti m.thechases.com wrote in message
news:ma******** *************** *************** @python.org...
>> I'm wondering if anyone knows of a way to extract data from a numeric
array along a line. I have a gridded dataset which I would like to be
able to choose two points and extract a 1-d array of the data values
along the line between the two points. Any ideas?

Are these all 45 degree lines or what?

If not, you'll have to use trigonometry and approximate the closest
matching cell. (Don't worry, Python has maths functions!! :))


There are a couple of optimal/easy cases where the run is horizontal,
vertical, or as you describe, 45-degrees (where x_step = y_step or x_step
= -y_step)

Any other set of arbitrary points, and you will have to specify the
behavior a little more finely.

You can use something like Bresenham's algorithm to march either "over" or
"up and over" to just pick out the "one point at each step that falls
closest to the actual line" along the run. There's also the Wu
Anti-aliasing line algorithm which takes something akin to Bresenham's
algorithm and then samples the potential points to yield an "anti-aliased"
result which averages the two potential data-points (traditionally
colors, but they could really be any average-able data values).

I'm not sure I've seen either of them implemented in Python before (though
actually *looking* for them might increase those odds ;-)

http://en.wikipedia.or g/wiki/Xiaolin_Wu's_li ne_algorithm

has details and a pseudo-code implementation (which shouldn't be too far
from the Python code). It's also got links to stuff on Bresenham's.

-tkc

No need for Bresenham's algorithm here. The point behind BA is to draw a
connected line using best selection of points, going cell-by-adjacent-cell
through the pixel map, so that you actually get a solid line in the result.
The OP's problem is much simpler - he/she (is "Theresaak" a boy name or a
girl name?) just wants the array of grid coordinates between a starting and
ending cell, so "continuousness " of the line is not an issue.

-- Paul
Oct 13 '06 #5
"Paul McGuire" <pt***@austin.r r._bogus_.comwr ote in message
news:kd******** ***********@tor nado.texas.rr.c om...
"Tim Chase" <py*********@ti m.thechases.com wrote in message
news:ma******** *************** *************** @python.org...
>>> I'm wondering if anyone knows of a way to extract data from a numeric
array along a line. I have a gridded dataset which I would like to be
able to choose two points and extract a 1-d array of the data values
along the line between the two points. Any ideas?

Are these all 45 degree lines or what?

If not, you'll have to use trigonometry and approximate the closest
matching cell. (Don't worry, Python has maths functions!! :))


There are a couple of optimal/easy cases where the run is horizontal,
vertical, or as you describe, 45-degrees (where x_step = y_step or x_step
= -y_step)

Any other set of arbitrary points, and you will have to specify the
behavior a little more finely.

You can use something like Bresenham's algorithm to march either "over"
or "up and over" to just pick out the "one point at each step that falls
closest to the actual line" along the run. There's also the Wu
Anti-aliasing line algorithm which takes something akin to Bresenham's
algorithm and then samples the potential points to yield an
"anti-aliased" result which averages the two potential data-points
(traditional ly colors, but they could really be any average-able data
values).

I'm not sure I've seen either of them implemented in Python before
(though actually *looking* for them might increase those odds ;-)

http://en.wikipedia.or g/wiki/Xiaolin_Wu's_li ne_algorithm

has details and a pseudo-code implementation (which shouldn't be too far
from the Python code). It's also got links to stuff on Bresenham's.

-tkc

No need for Bresenham's algorithm here. The point behind BA is to draw a
connected line using best selection of points, going cell-by-adjacent-cell
through the pixel map, so that you actually get a solid line in the
result. The OP's problem is much simpler - he/she (is "Theresaak" a boy
name or a girl name?) just wants the array of grid coordinates between a
starting and ending cell, so "continuousness " of the line is not an issue.

-- Paul
Hmmm, maybe I was a little hasty. Re-reading the OP's post, he/she may in
fact want the Bresenham-type traversal of the grid from point A to point B.

Need a little more info on requirements, Theresaak.

-- Paul
Oct 13 '06 #6
On 10/13/06, Paul McGuire <pt***@austin.r r._bogus_.comwr ote:
"Theerasak Photha" <ha********@gma il.comwrote in message
news:ma******** *************** *************** @python.org...
On 13 Oct 2006 07:33:17 -0700, bc*******@gmail .com <bc*******@gmai l.com>
wrote:
Hi everyone,

I'm wondering if anyone knows of a way to extract data from a numeric
array along a line. I have a gridded dataset which I would like to be
able to choose two points and extract a 1-d array of the data values
along the line between the two points. Any ideas?
Are these all 45 degree lines or what?

If not, you'll have to use trigonometry and approximate the closest
matching cell. (Don't worry, Python has maths functions!! :))

-- Theerasak

No need for that messy trig stuff - simpler and faster would be to use
linear interpolation, since you know the starting and ending cell
coordinates, and how many values you want, compute the array of... oh, hell,
here it is:
I'm not in kolluge yet and I just learned about linear interpolation
today---although I don't think it would necessarily apply to this
problem, where the increments set by the grid might be more discrete
than the line itself (hope you see what my innumerate self is trying
to say!!), thanx for teaching me something new. :)

To myself: Gee duh, Theerasak, it make sense now!

-- Theerasak
Oct 13 '06 #7
Theerasak Photha wrote:
I'm not in kolluge yet and I just learned about linear interpolation
today---although I don't think it would necessarily apply to this
problem, where the increments set by the grid might be more discrete
than the line itself
that's usually solved by stepping along the grid in one direction, and
interpolating in the other.

</F>

Oct 13 '06 #8
On 10/13/06, Fredrik Lundh <fr*****@python ware.comwrote:
Theerasak Photha wrote:
I'm not in kolluge yet and I just learned about linear interpolation
today---although I don't think it would necessarily apply to this
problem, where the increments set by the grid might be more discrete
than the line itself

that's usually solved by stepping along the grid in one direction, and
interpolating in the other.
Could rounding still be an issue? I sketched a line w/ m = 1/3 and
could see different possible outcomes for certain points on the line
in between each 3rd point on the x axis.

-- Theerasak
Oct 13 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

226
12500
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> d1 = {'a':1} >>> d2 = {'b':2} >>> d3 = {'c':3}
1
1994
by: Tim Bradshaw | last post by:
I'd like to be able to install python with stow, and then to install various modules which use distutils, also with stow. This currently pretty much won't work, because Python chases symlinks to set sys.prefix, which means that the site path gets set to the `true' location rather than the one with all the links. This means that Python won't find modules you install with stow, unless you glue the linky site directory into sys.path. Doing...
1
1976
by: youngdubliner | last post by:
I'm having a problem ........ I've stripped all my code to help isolate the problem. Its seems to be with importing numarray when python is embedded in C. I have a simple C program it Opens Python imports a script and then Closes Python like so .......
7
3002
by: Jive | last post by:
Here's my sitch: I use gnuplot.py at work, platform Win32. I want to upgrade to Python 2.4. Gnuplot.py uses extension module Numeric. Numeric is now "unsupported." The documentation says "If you are new to Numerical Python, please use Numarray.". It's not that easy, dangit. The download page for numpy does not contain a 2.4 version of Numeric, and I suspect they do not intend to release one, because there IS a 2.4 version of...
0
1517
by: Cedric | last post by:
This is a 3 weeks old problem, but having found a solution (and having looked for one here, finding only this message), I'm replying now. From: Jive (someone@microsoft.com) Subject: Upgrade woes: Numeric, gnuplot, and Python 2.4 Date: 2004-12-11 18:45:10 PST > Here's my sitch: > > I use gnuplot.py at work, platform Win32. > I want to upgrade to Python 2.4.
2
3101
by: martino | last post by:
Hi, I am trying to install NumPy (v23.8) onder Macosx (panther version). Python (v2.3) is bundled into panther and I installed the IDE on top of that. After having untarred the source distribution in the desktop directory and typed >python setup.py install as recommended in the attached documentation, I get: running install
32
2517
by: David | last post by:
Hi I'm trying to teach myself python and so far to good, but I'm having a bit of trouble getting a function to work the way I think it should work. Right now I'm taking a simple program I wrote in Fortran and trying to do it in Python. I got it to work, but now I'm trying to modularize it. My fortran program uses a subroutine and I was trying to do the same thing in Python. But I'm still new so I'm having trouble understanding what I'm...
0
2107
by: ronysk | last post by:
Hi, I am posting here to seek for help on type conversion between Python (Numeric Python) and C. Attachment A is a math function written in C, which is called by a Python program. I had studied SWIG and Python/C API a bit. I was able to pass numeric array (Numeric Python) into the C function and access/change these arrays. Problem I am having is that I couldn't quite get the array converted back to some PyObject or something that...
28
18447
by: DaemonCoder | last post by:
The challenge is to create the shortest algorithim, In any programming language. RULES: 1. All code must be on one line. 2. Languages that prevent this are disallowed. I.E Assembly, and of course COBOL. All other languages are fair game. 3. Sequence must go up to at least 4181. If i missed a languge that should be disallowed please let me know... My solution is in Java. weighing in @ 108 Characters w/o spaces.
0
8266
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8199
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8705
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.