Assign regions (not points) to a node, then search for which nodes' regions a point is in

Events happening in the community are now at Drupal community events on www.drupal.org.
martin_q's picture

I feel sure that all or almost all of what I need is already available, but so far I can't find it. And I am willing to have a go at coding whatever is missing.

I have a custom node type which represents a human language. So I want to define what regions a language is spoken in. They can be quite vague: circles would do, or polygons. But in any case, we must assume that a language is spoken in one or more regions, and individual points make little sense.

Then I want site visitors to be able to search for a place on a map (point and click, or perhaps type the name of the location) and next to the map I would like to display a list of all the nodes whose regions cover (or are near) the point the user has specified.

I don't really want to display the regions on my map; I don't want to start making [political] claims about what is spoken where. So it's fine if it's quite vague, simply a list of "here are the languages we found near to where you clicked".

So far I have installed gmap and location modules and I feel like I've got 90% of the way, though I don't yet fully understand how to customise them, e.g. to allow content editors to specify circles or polygons instead of points for a node's location.

I'm keen to hear gut reactions, half-baked ideas, anything which I can pick up and work on further. To a certain degree, this is my day-job, so I can allocate a fair bit of time to it over the next few weeks, but the clock is ticking on how long I am free to do that for. Bear with me as I ask the stupid questions to begin with; I'm learning to be a fast learner in Drupal development...

Thanks in advance for any suggestions, ideas or other inspiration you can offer.

Comments

This is exactly what the Geo

becw's picture

This is exactly what the Geo module does: it lets you use geographic features (polygons, points, lines) in a CCK field. You can either load in a shapefile and use features from that, or you can enter custom geographic features (currently the interface is fairly primitive: you enter "Well-known text" (WKT) directly). I've also written a CCK widget that lets you enter a Geo point via a GMap--I haven't put it up on d.o yet, though. Another cool widget would be one that allowed you to draw a polygon on a map instead of entering WKT. As far as geographic search goes, that should be part of Geo at some point (might already be?), since a significant reason for Geo's existence is to take advantage of geospatial database features.

On the other hand, with GMap and Location, you can do a less-formally-correct implementation... with Location+GMap, you can allow users to enter lat/lon pairs by clicking on a map. There's a method of proximity search via Views. The GMap Macro Builder allows users to draw circles or polygons on a map, but it only generates a "GMap Macro" in a text field--it's not a cck input widget or anything, and it doesn't save the geodata in a searchable way--anyway, if you're going to store polygon data you should use Geo.

Hope this helps--I'd be happy to hash this out more if you want!

Hmmm, I guess I need to bite

martin_q's picture

Hmmm, I guess I need to bite the bullet and examine CCK some time - haven't touched it yet, and I'm suddenly wondering if I've reinvented the wheel with my custom module as a result! So I may or not be ready for your CCK widget some time. I don't think I can hope for WKT from my users...

Geo looks cool, but also looks like it's not finished yet. Is it safe to install on my own testbed site? Am I asking for trouble if I attempt to use the features it does already do, on my production site (when I have one!)? I'm guessing 'yes'. What sort of timescale are they working to?

With GMap and Location, I've got as far as making it possible to enter lat/lon pairs by clicking on a map (although these do not show up to anyone other than the Admin user at the moment - any idea why?). Tell me more about the proximity search via Views (Views is another thing I haven't looked at yet!). I'm guessing (having looked at chicagoancestors.org) that the proximity is defined per search. How about if it was defined per node, i.e. each node has a lat/lon pair and a proximity value that means it appears in search results if the search point is within its proximity range. This is perhaps just a back-door way of implementing circles, but if it can be done, it would do the job. Is it worth me trying to re-engineer the proximity search to work like that, or is it not as simple as that?

Thanks for getting me started with these ideas, though: I'm definitely interested in hashing them out further! :)

Cheers,
Martin

re: ...

becw's picture

CCK -- it is very cool; you can not only add different fields to content, but you can customize the fields' input widgets and output display. I'm guessing your custom module does something like what Location does, and adds stuff to nodes independently of CCK? On the other hand, it can be pretty hard to jump into coding for CCK...

Geo -- I don't think anyone can hope for WKT from their users! But at this point Geo is definitely something to play with... as far as timeline goes, Allie Micka did a big chunk of work on it this August, and Bdragon might be getting a chance to work on it a little later this fall. I don't really have anything more specific than that, for now.

regular users entering lat/lon pairs with Location -- there's a "submit latitude/longitude" permission that allows users other than uid=1 to enter lat/lon pairs.

proximity search -- well, first of all, Views: Views is powerful, sometimes a hassle, and THE widely accepted way of filtering and organizing nodes for display (not just nodes but also users, terms, etc). Location provides a proximity "filter" for Views; you may "expose" the filter to users to allow them to enter a lat/lon. Location then provides a proximity "field" for Views which displays the distance between the filter lat/lon and the node's Location lat/lon. So it's very point-oriented. I think for what you're talking about--a the location of a 'language' node having a point and a radius around that point--the standard location search won't work. Location's proximity search is extremely basic...

adding gmap macro to a view with proximity search?

martin_q's picture

OK, leaving aside the modified proximity thing for a moment, I'm getting to grips with Views, I think. So how do I incorporate a gmap macro into the view for it to be clicked on and thus provide the co-ordinates for the View filter?

Also, can I add custom sql to a view?

Trying not to ask stupid questions here, but I can't find the answer to this yet.

try location: proximity map

cglusky's picture

select location: proximity map as filter; set your default center point; expose it

r,
coby

"location: proximity map" is not an option

martin_q's picture

Hi, thanks for your suggestion.

However, I don't have location: proximity map as an option on my screen. I have location and gmap installed. Do I need anything else? I am using D6.5 by the way.

Thanks,
Martin

Sorry that was for D5

cglusky's picture

Have not gotten around to gmap/location/geo in 6 yet. It would be a filter option when creating a new view.
R,
Coby

So how can I help?

martin_q's picture

OK. If there is anything I can do to help speed that along, please let me know. I've spent the last couple of months coding some custom modules for D6, so I feel I could at least have a go at some porting or something.

Proximity search with Views

martin_q's picture

This proximity search is sounding good. OK, so I was talking points and radii because I imagined that was how the proximity search would work. Forget that, then. Instead...

What is to stop me allowing my nodes to have (one or more) lat/lon pairs plus some kind of max_distance value. Then I modify my proximity search to calculate the distance between the filter lat/lon and the node lat/lon... and to calculate whether that distance is less than the max_distance specified for the node's lat/lon pair. This is functionally no different from having a point with a radius, but is actually considerably easier to calculate. I'm going to investigate and see if it can be done!

Most of the way there! How to add the gmap?

martin_q's picture

In the last 24 hours or so I have sat down and taught myself Views and the Views API. I now have a working filter which I've called "in_range".

Using location and gmap, nodes can be assigned one or more locations by clicking on the gmap to get lat and long. I have (temporarily) used the 'additional' field (actually intended for additional lines of an address) to enter a numerical value which is essentially the radius of the circle I want to imagine on my map. I'm now calling that range.

My views filter takes three arguments - latitude, longitude and distance, and it looks for nodes that are less than distance+range of the point entered by latitude/longitude. (So, distance is measured from where I'm stood and range is measured from where each node is. I guess both of those are pretty vague names.) Anyway, I managed to get this working by borrowing from the currently un-used location_views_handler_filter_proximity.inc and using the following query code:

$this->query->add_where(
  $this->options['group'],
  earth_distance_sql($lon, $lat, $this->table_alias) ." < $this->table_alias.additional + %f",
  $distance_float
);

And it works!

OK, so my question is pretty simple in comparison:
At the moment the View page has three boxes to enter lat, long and a distance. How do I change this to incorporate a gmap macro instead, so that the click supplies lat and long to the View filter? (I'm also thinking about having it supply information about the zoom level and somehow defining distance from that. Or I might just ditch the distance thing.) I know how to write the macro, I just don't know where to put it or how to get it to pass its output to the View filter.

Later questions will include how to alter the form to add my own "range" field, but that can wait for now.

Thanks as ever.

Location and Mapping

Group organizers

Group categories

Wiki type

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: