Hi there,
does anyone knows if there is a way to extract the LON/LAT saved by Geo Field from the database? I used it to store Positions and now I need the positions in another DB table. Checking the field in content_type_MYTYPE table shows, that the data is stored as "geometry". Is there a way to get the lon and lat values from that?
Any help would be really appreciated!
Tom

Comments

allie micka’s picture

Status: Active » Closed (fixed)

Ideally, you're using "straight drupal", and you can use the Lat / Lon formatters or view fields. If you really must access this information manually, you can do something like this:

SELECT X(field_MYFIELD_geo) AS lon, Y(field_MYFIELD_geo) AS lat FROM content_type_MYTYPE;

thommyboy’s picture

Wow- that was a quick answer ;) Actually I need an SQL-solution because I need to put the "clean" data into another table
(I'm migrating data containing geo-fields to gmaps)
It worked well, thank you!

jonathan_hunt’s picture

Version: » 6.x-1.0-alpha5

I have a geo field storing a point, entered as lat lon. I've selected latitude in the CCK display field settings for the geo field but don't get any output on node view. Ditto for selecting longitude. Is there something I'm missing?

So, it's not clear that the lan/lon formatters work. If I have the node data, e.g.

[field_geo_latlon] => Array
        (
            [0] => Array
                (
                    [geo] => ?U???N?e?˸??E?
                )
        )

how do I extract it?

jonathan_hunt’s picture

Status: Closed (fixed) » Active

I guess this won't get attention if it's still closed...

bflora’s picture

Priority: Normal » Major

It's unclear how to use the lat/lon formatters. I'm in the same boat as Jonathan Hunt. I have a cck geo field that's getting LatLon fed into it. Now I need to know how to call to that data so I can put it where I want in my theme.

When I print out $node->field_coordinates[0][geo] I get a bunch of gibberish like in comment #3 above.

I suspect there are some functions in the geo module that I can run to output the actual lat/lon numbers. What are they and what exactly do I feed them? $node->field_coordinates? $node->field_coordinates[0]? $node->field_coordinates[0][geo]?

Thanks!

thommyboy’s picture

hi guys,
have a look at #1 and http://dev.mysql.com/doc/refman/5.1/de/point-property-functions.html the data is stored as geometry-type in mysql i think...

gagarine’s picture

Priority: Major » Normal

Display a field using $node->field_XX[0][value] is never a good idea although it's a super common mistake. It's often a security issue except if you have a [view] in the array (that mean the node is already passed by the rendering functions). Read http://growingventuresolutions.com/blog/drupal-security

To build a single field use content_format

print content_format('field_content_something', $node->field_content_something, $formatter = 'default', $node = NULL);

You can now use the long lat formatter as say Allie in #1.

P.S. @bflora I think putting a support request in major state is not so appropriate. If it's really critical for you, Allie Micka is certainly open for paid support...

bflora’s picture

Hey gagarine,

Thanks for the tips on best practices. I appreciate it.

Can you give me an example of how to use the long lat formatter from #1? I understand I'm supposed to use that function, but I don't understand what I need to feed it. What does it take as an input. The function just says "$element" and that doesn't tell me much.

For example, I've tried adding each of these three tags to my templates.

print theme_geo_latlon($node); (generates a PHP error)
print theme_geo_latlon($node->field_coordinates[0]); (nothing displays)
print theme_geo_latlon($node->field_coordinates[0][geo]); (displays some non-English characters)

So these function calls do not appear to be working. I've tried about 10 other variants and functions. If there's someone who knows how to do this properly, could they paste in the exact PHP code that would work for a geo field called field_coordinates? I can't find anything in the code or documentation that explains how to do this. Thanks.

gagarine’s picture

You don't need to pass by the theme function but directly use content_format. So CCK do everything for you. That works with every CCK fields.

Formatters are declared in geo_field_formatter_info. Badly they are no latlon formatter but only one lat and lon. I make a patch about the lack of this formatter #596692: add geo microformat as output widget?.

So the code (I didn't try it but I hop it works ;) ):

$formated_lat = content_format('field_coordinates', $node->field_coordinates[0], 'lat');
$formated_lon = content_format('field_coordinates', $node->field_coordinates[0], 'lon');
print $formated_lon . ' ' . $formated_lat ;

field_coordinates need to be the name of your field of course.

bflora’s picture

I tried

$formated_lat = content_format('field_coordinates', $node->field_coordinates[0], 'lat');
$formated_lon = content_format('field_coordinates', $node->field_coordinates[0], 'lon');
print $formated_lon . ' ' . $formated_lat ;

and

$formated_lat = content_format('field_coordinates', $node->field_coordinates, 'lat');
$formated_lon = content_format('field_coordinates', $node->field_coordinates, 'lon');
print $formated_lon . ' ' . $formated_lat ;

And neither of them displayed anything. I checked and field_coordinates is the proper name for my field.

Other suggestions?

thommyboy’s picture

did u try $formated_lat = content_format('field_coordinates', $node->field_coordinates[0][geo], 'lat');

bflora’s picture

That threw a fatal error.

vlad.k’s picture

subscribing

gagarine’s picture

Title: Extract LON/LAT from data written by geofield » Using content_format with lat or lon formater doesn't work
Category: support » bug

Ok perhaps they are a problem.

I change to bug because it sound wired for me but hop it's only a typo in my example or a problem with the @bflora's $node.

I will try soon. If nobody answer in 2-3 day just shout me a private message.

ahtih’s picture

I verified that the code above does indeed not work in many situations, because theme_geo_formatter_lat() expects input to be in Geo 'array' format, e.g.

[field_geo_latlon] => Array
        (
            [0] => Array
                (
                    [array] => Array
                        (
                            [lat] => 11.111
                            [lon] => 22.222
                        )
                )
        )

In reality, in many contexts $node->field_geo_latlon is in different formats; for example, node_load() returns data like this:

[field_geo_latlon] => Array
        (
            [0] => Array
                (
                    [geo] => —ij¢‘ 9@'‡©Ëí/M@
                    [gis type] => point
                    [wkb] => —ij¢‘ 9@'‡©Ëí/M@
                    [wkt] => POINT(25.627222222222 58.374444444444)
                )
        )

..and in hook_nodeapi($op = 'update') it is in yet another format (which is a database-specific format returned by GeomFromText() function in geo_field($op='presave'); this is not WKB in neither MySQL nor PostgreSQL, although similar to WKB in both):

[field_geo_latlon] => Array
        (
             [0] => Array
                (
                    [geo] => 0101000020E610000097C4B3A291A039402787A9CBED2F4D40
                )
       )
gagarine’s picture

I thinks I get where is the problem.

in geo.module the hook geo_nodeapi they is this code

    case 'view':
      // Add a #pre_render callback to convert geo values to a format
      // understood by the theme function in use by that field.
      $node->content['#pre_render'][] = 'geo_field_convert_value';
      return;

So how I get it is than every field go at the end in this function to be converted before they are themed. I don't really understand why is like that.

Why not doing basic format transformation in geo_field.module in the hook_field on the load operation so other module can use the data. Those transformations cost a lot? If not we can imagine having more format in the field array...

gagarine’s picture

here one way to get an array with lon and lat.

        $element = $node->field_location_geo;
        $format = 'array';
        $values = geo_value($element[0]['wkb'], $format,'wkb');