Generate a grid of cells, and give characteristics

  • Thread starter Joop Van den tillaart
  • Start date
J

Joop Van den tillaart

Hi you all I am new to this forum and also quite new to ruby...I have
some PHP knowledge but I'm definitely no pro in programming...

Now I have a project at school...I will try to explain it as short as
possible...

I need to generate a grid, let's say of 196 cells (grid of 14x14), I'm
now generating it with an array:

grid = [1, 2, ..., 195, 196]

1. first of all is there a way to somehow automatically generate an
array of following numbers or other characters because when I later on
plan to change the gridsize it would be nice if this can be filled
automatically...

2. The grid is then intended to be used for a land use project...first
of all I have to give particular cells the characteristics of
infrastructure, for instance all the cells on the right hand side (cell
14, 28, 42, 56 etc.) of the grid form a road from top to bottom...

3. when there are enough roads for a good infrastructure (to be judged
by myself) i need to fill the open areas with houses...each cell in
these areas should have some location characteristics like distance to
infrastructure...when a cell meets some demands it can then be located
as a suitable cell for building some type of house on it...

I see that this is a bit much too ask in one time and I'm not expecting
a totally worked out answer to all my questions but maybe someone can
help on the way and after that I can come back if I encounter
problems...

Thanks very much you all, hope you can help in any way!

Greets
 
H

Harry Kakueki

Hi you all I am new to this forum and also quite new to ruby...I have
some PHP knowledge but I'm definitely no pro in programming...

Now I have a project at school...I will try to explain it as short as
possible...

I need to generate a grid, let's say of 196 cells (grid of 14x14), I'm
now generating it with an array:

grid = [1, 2, ..., 195, 196]

1. first of all is there a way to somehow automatically generate an
array of following numbers or other characters because when I later on
plan to change the gridsize it would be nice if this can be filled
automatically...

Hi,

If you want to use one array like in your example, you can fill it
using a range.

http://www.rubycentral.com/book/tut_stdtypes.html#S3

grid = (1..196).to_a
p grid

Is that what you want to do?

But you may want to use a 2D array.

Harry
 
J

Joop Van den tillaart

Harry said:
Hi,

If you want to use one array like in your example, you can fill it
using a range.

http://www.rubycentral.com/book/tut_stdtypes.html#S3

grid = (1..196).to_a
p grid

Is that what you want to do?

But you may want to use a 2D array.

Harry

Hey man, thanks for replying...okay so I already learned something...

grid = (1..196).to_a
p grid

And I studied some other thread:

http://www.ruby-forum.com/topic/67248#83349

I typed over (sometimes I type over because I think it's better to
understand that way) one of the last scripts and it lets me have control
to print particular cells of an excel like sheet...

Maybe it is easier for my project to make a grid like: A1 - A14, B1 -
B14...N1 - N14) so the horizontal rows of the grid are alphabetical and
the columns numeric...Is this really an array and can I after generating
it alter it in some way...

For example...i run that script...and after that can I say the cells
A14, B14, C14, .. , M14, N14 (the most right column) have to be
characterized as a road?

Man I'm way to new to this I think I need a long time to learn.. :(

If anyone can be of help it would be higly appreciated...
 
J

Joop Van den tillaart

Harry said:
How much time do you have for your project?

Harry

Well I don't really have a deadline but I'm hoping to go as fast as I
can...and then I hope to be done with it in about 8 - 10 weeks...
 
J

Joop Van den tillaart

Harry said:
Is this a Ruby project?

You may want to take a look at this.
http://pine.fm/LearnToProgram/

Then try writing some code.


Harry

Okay...I followed through the link you gave me...and i found another
thread dealing with Array en Grid:

http://www.ruby-forum.com/topic/88174#167392

In the last two reactions I think i found a starting point for my
problem...

I replaced the numbers so now I have this:

require 'enumerator'

@array = (1..196).to_a
@grid = Array.new(14) {|idx| Array.new(14)}

@array.each_with_index do |val, idx|
@grid[idx/14][idx%14] = val
end

p @grid

I then get to have an array from 1..196 and those are divided in a grid
of [[1..14],[15..28], .. ,[183..196]]

What I want to do next is work with these cells...make them 'live' so to
say...

Do i need to put this array in some sort of database? So i get to have
some columns where one column represents the cells numbers 1..196 some
other columns then need to have some cell characteristics, and with
these characteristics and a formula (which i already have) the cell
types need to be assigned (infrastructure, housing, facilities etc.)

Can anyone help me any further? I would be very grateful for the help...
 
T

Todd Benson

Do i need to put this array in some sort of database? So i get to have
some columns where one column represents the cells numbers 1..196 some
other columns then need to have some cell characteristics, and with
these characteristics and a formula (which i already have) the cell
types need to be assigned (infrastructure, housing, facilities etc.)

What's your motivation? To just get it done and working (I'm a little
confused as to what "it" is, though)? To learn Ruby or OOP? Is this
a one-off (used just for this) or to be expanded and used for
something else (are you building a script or an app, for example)?
Are you automating data construction, manipulation, or evaluation, or
all of the above? You say you have formulas for characteristics of
the each of the 196 "plots". Do these formulas depend solely on x and
y coordinates (i.e. all plots with x == 4 have a creek running through
them, all plots with y == 13 are next to power lines, etc.) or do you
plug this info in for each plot one at a time? Is it permanent info?

Should you use a database? It depends. I'm a database freak, so I
probably would use one even if it required some extra effort (but I'm
well versed in SQL, so that part would be easy; there are some DB
engineers out there that would be more than happy to encode your
application entirely in the DB, complete with distance calculations,
autogeneration of street addresses, the whole works! But that's no
fun ... more like trying to fit a square peg in a round hole.

By the sound of it so far, I'd start with a database containing the
unique and rare characteristics of each plot and then "script" into
the DB the more general characteristics using SQL and/or Ruby. Then
I'd build some generic classes so that I can alter data in the
database, maybe a Site class (of which you will have only one
instance), a Plot class, a Road class, etc. If you want to use Ruby
only, I recommend using YAML for your persistent data. Or, if you
want to stick with a database but avoid SQL, there are some great ORM
and Ruby/SQL bridge tools out there, like ActiveRecord and Sequel
(neither of which I've tried).

There are tons of ways to model this in Ruby; here's one cowboy way I
might try with no regard to an external database -- just playing with
data in the program:

class Plot
@@neighbors = []
attr_accessor :x, :y, :characteristics

def @@neighbors.[]( x, y )
self.select {|n| n.x==x and n.y==y}.first
end

def @@neighbors.used?( x, y )
self[x,y]
end

def < new_chars
@characteristics.merge! new_chars if new_chars.is_a? Hash
self
end

def self.community
@@neighbors
end

def initialize( x, y )
raise Exception.new("Property at (#{x},#{y}) already
initialized!") if @@neighbors.used?( x, y )
@x = x
@y = y
@characteristics = {}
@@neighbors << self
end

def to_s
"Plot <#{@x},#{@y}>: #{@characteristics.inspect}"
end
end

#####
# Begin little program to play with data

# build a 196 square unit grid of plots
14.times {|x| 14.times {|y| Plot.new(x, y)}}

# grab the plot object at (7,13) with newly defined [] method
# print it two different ways
puts "inspect => #{Plot.community[7, 13].inspect}"
puts "puts => #{Plot.community[7, 13]}"

# add some characteristics to a plot using the newly defined < method
# and a hash
puts "After setting some properties:"
puts Plot.community[7, 13] < { :name => "Ruffle Gorge", :electricity => "no" }

# add electricity
puts "After installing electricity:"
puts Plot.community[7,13] < {:electricity => "yes"}

# add a whole new plot off main grid
Plot.new(21,23) < { :name => "Scrooge's Stooge", :water => "no" }
puts "New plot:"
puts Plot.community[21,23]

I'm not suggesting you do something like this ... just to get you
thinking is all. A more appropriate way would be to have a Site
object that contains Plot objects that know their locations -- like
above, but without @@neighbors, and its methods #[x, y] and #used/
placed in the Site class instead.

Todd
 
J

Joop Van den tillaart

Thanks Todd, I will try your code after commenting on your help and
questions :)


Well the motivation is a project at my university. On a larger scale the
project is about having a plan area that's in need of development: those
engineers need a program or application that let's you put in some data
in a gridstructure...like for instance a cells distance to a road when
this distance is small they are more likely to be assigned for placing a
house on. Are they further from the road cell maybe they are more likely
to be assigned as nature or something like that...

But my project isn't to work it out in all perfection. First I must
investigate how such a application could be made working. It's all in
the first stages and next to that it's for me useful to come in contact
with ruby. Also maybe when this system is generally working i will try
to get it in google sketchup which has ruby implemented. That way we
want to try to make the grid, with all the assigned functions to the
cells, visible by drawing it.
When this system is working good then it will be passed on to others and
they will most likely extend it further or maybe make improvements...so
the project for me is very free, to try and come up with something and
to get some experience in ruby..

So me and my tutor thought of making some grid like plan area. To have a
beginning point we add some roads by hand and all the remaining cells
then have at least one characteristic: distance to road. Some other
characteristics also need to be given for each cell. For now im not sure
if these will be put in by hand or some other way...

The calculations i was talking about are just calculating for each cell
the suitability for placing houses and this suitability depends on the
characteristics of each cell...

Sorry if im not totally clear, it's just the combination of too little
knowledge of the capabilities of ruby and my bad english ;-)

Thanks again Todd...maybe later today or tommorrow (when I have a
meeting with my tutor) I will try to explain it better...I also have a
explaining document of a comparable project but I think no one is really
interested to read it through (20 pages haha)
 
T

Todd Benson

Thanks again Todd...maybe later today or tommorrow (when I have a
meeting with my tutor) I will try to explain it better...I also have a
explaining document of a comparable project but I think no one is really
interested to read it through (20 pages haha)

I'd be interested in seeing that document. Email me directly off the
list if you get a chance. I'm working on a similar personal project
involving land use and would like to see what direction you take
whether it involves Ruby or not.

Todd
 
J

Joop Van den tillaart

Todd said:
I'd be interested in seeing that document. Email me directly off the
list if you get a chance. I'm working on a similar personal project
involving land use and would like to see what direction you take
whether it involves Ruby or not.

Todd

Well it is a project about land use so I'll mail it to you...
 
J

Joop Van den tillaart

Is there anyone else who has ideas on this or is my explanation not
clear enough maybe?

Thanks in advance...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top