Can someone please help with code on how to use a computed cck field to return the closest zipcode (just one result) to a location latitude and longitude. I have tried several ways but keep getting NULL. My computed field is called computed_zipcode and I am storing it as an INT with a data lenght of 7. Working with databases is way over my head.

Thank you in advance;
Paul

Comments

pwieck’s picture

Title: Computed cck zipcode filed from location coordinates » Computed cck zipcode field from location coordinates
pwieck’s picture

Please help. I am stuck on this one.

farald’s picture

Title: Computed cck zipcode field from location coordinates » Get location coordinates from inside computed field

Paulwamail, I believe we are having sort of the same problem. I am trying to count the number of articles of content type dblog in nearest proximity.
So, Ive comed up with this (not entirely perfect) code:


$min_lon = ($node->location['longitude'] - 1);
$min_lat = ($node->location['latitude'] - 1);
$max_lon = ($node->location['longitude'] + 1);
$max_lat = ($node->location['latitude'] + 1);

$result = db_query("SELECT COUNT(node.nid) AS nid
 FROM node node 
 LEFT JOIN location_instance location_instance ON node.vid = location_instance.vid
 LEFT JOIN location location ON location_instance.lid = location.lid
 WHERE (node.type in ('dblog')) 
 AND (location.latitude > %f AND location.latitude < %f 
 AND location.longitude > %f AND location.longitude < %f)", $min_lat, $max_lat, $min_lon, $max_lon);
 
 $items = array();
while ($row = db_fetch_object($result)) {
  $items[] = $row->nid;
}
$node_field[0]['value'] = $items[0];
 
;

The problem is, exacly as yours, I cannot get an answer from the $node->locations[0][latitude] OR $node->location[latitude] fields, other than NULL.

So, Im pretty much stuck too. If I override the input values with this:

$min_lat = 63.4 - 1;
$min_lon = 10.39 - 1;
$max_lat = 63.4 + 1;
$max_lon = 10.39 + 1;

The script outputs fine.

So, what is needed to get the locations lat/lon of node from inside a computed field?
Thanks:)

netbear’s picture

And what is there if you print

<pre>
print_r($node);
</pre>

in your node template?

farald’s picture

Title: Get location coordinates from inside computed field » Computed cck zipcode field from location coordinates

Oh, I see that we are working on different drupal versions. I do not know if the above works for 5.x, but at least maybe you got some clue on how to create your db query:)

farald’s picture

<pre>
print_r($node);
</pre>

Will have a look.

farald’s picture

Title: Computed cck zipcode field from location coordinates » Get location coordinates from inside computed field
[location] => Array
        (
            [lid] => 12
            [name] => 
            [street] => 
            [additional] => 
            [city] => 
            [province] => 
            [postal_code] => 
            [country] => us
            [latitude] => 63.326090
            [longitude] => 10.113290
            [source] => 1
            [is_primary] => 0
            [locpick] => Array
                (
                    [user_latitude] => 63.326090
                    [user_longitude] => 10.113290
                )

This one indicates the use of $node->location['latitude'], and this one:

[locations] => Array
        (
            [0] => Array
                (
                    [lid] => 12
                    [name] => 
                    [street] => 
                    [additional] => 
                    [city] => 
                    [province] => 
                    [postal_code] => 
                    [country] => us
                    [latitude] => 63.326090
                    [longitude] => 10.113290
                    [source] => 1
                    [is_primary] => 0
                    [locpick] => Array
                        (
                            [user_latitude] => 63.326090
                            [user_longitude] => 10.113290
                        )

                    [province_name] => 
                    [country_name] => United States
                )

indicates a $node->locations[0]['latitude'] when only one location.

So, this leaves me with no idea why I can't reach the value from computed field.

Maybe find a way of somehow sneaking print_r into a computed field to see what values are present in the field.

netbear’s picture

may be this is weights problem and when the computed field is doing its job there is not location data yet. If so you need to set weight of computed field module havier than location module in system table.

farald’s picture

I have set the weight of locations to -10 at http://mysite.org/admin/content/node-type/dblog.
The computed field is at the very bottom of the list. Still nothing.

Thanks for suggestion though:)

Trying to use a cck location field instead, to see if that works better.

pwieck’s picture

I should be home Monday Jan 12 and will try to mess with it again. I had given up due to no response. I am working on a lake fishing site and I only have the latitude and longitude locations for the lakes. The weather program I am using and a script I have to find closest cities needs the zipcode. I thought it would be better to have cck compute the zipcode and store it in the database instead of manually looking up and entering over 2000 zipcodes. I am using the HamWeather script for my weather and the people there have been very helpful. They wrote me a query to use my lat and long pairs and search their data in my database for the closest city for the weather program to use. I think I may be able to modify that query to work inside the location zipcode data.

I have not dug that deep into this problem because I thought it would of been a one, two knock out for someone to answer. I have had most all my posts go unanswered here on drupal.

I thank you all for your time and interest.
Paul

pwieck’s picture

Still no luck

Here is what I tried.

$lat = $node->locations[0]['latitude'];
$lon = $node->locations[0]['longitude'];

$result = db_query("SELECT a.zipcode, (@distance:=(3963*ACOS(SIN(a.lat/57.3)*SIN($lat/57.3)+COS(a.lat/57.3)*COS($lat/57.3)*COS($lon/57.3 - a.lon/57.3)))) AS distance FROM places as a WHERE a.lat<>'' and a.lon <>'' ORDER BY distance limit 0,1");

$node_field_item['value'] = db_fetch_object($result);

The display format is $display = $node_field_item['value'];

And using <?php print $node->field_testcomp[0]['value'] ?> to show the result

I get nothing printed back.

farald’s picture

That query should indeed do the trick.Have you tried putting in a dummy lat/lon to see if the query works well?
If yes, your problem is identical to mine, $node->locations[0]['latitude'] = NULL, which is quite odd..

I got around the problem for now by accessing the database every time the node is loaded (via content template), so that the value is there. My solution is of course in question when it comes to performance, but it will have to do the job for now.

pwieck’s picture

I went all the way to the basics and computed field still no show or won't work. I have all up-to-date modules on 5.14 drupal

1. Named field test_hello
2. In the code area I just put $node_field_item['value'] = 'hello';
3. Clicked Display this field
4. In display area put $display = $node_field_item['value'];
5. Tried both store in database with text, length 25, not null, sortable and not using database
6. In contemplate put <?php print $node->field_test_hello[0]['value'] ?>
7. Tried updating old cck node and making new cck node
8. Tried test server and live

Nothing....Goose Egg

Can someone help me. I am at my ends with this. Thanks

farald’s picture

Status: Active » Fixed

Finally!

I saved the whole $node query to the database, and found out that location values is indeed not saved in $node->location[0][latitude]. BUT, the new latitudes are stored in following:

$node->locations[0][locpick][user_latitude];
$node->locations[0][locpick][user_longitude];

So, to manipulate the locations from computed field, use these:)
However, I do not know what other location fields are called. To find out, just put

<?php
$node_field[0]['value'] = var_export($node, TRUE);
?>

in your custom field.
Let it save to the db as a 'text' type, save a node, then go to phpmyadmin and inspect the array.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.