The following snippet does not function. The geo field ends up being NULL. Any suggestions?

$node = node_load(arg(1));
$wkb = db_result(db_query("SELECT GeomFromText('POINT(20 20)', 4326)"));
$node->field_geofield[0]['geo'] = $wkb;
$node->field_geofield[0]['wkb'] = db_decode_blob($wkb);
node_save($node);

Comments

phayes’s picture

Status: Active » Closed (fixed)

Alright i've figured it out.. You need to pass it as wkt.

$node = node_load(arg(1));
$node->field_geofield[0]['geo'] = 'POINT(30 30)';
node_save($node);

phayes’s picture

Status: Closed (fixed) » Active

Re-opening. This breaks any geo field:

$node = node_load($nid);
node_save($node);

This is because when loaded, geofield[0]['geo'] contains wkb, but when saving, it expects it in wkt.

phayes’s picture

StatusFileSize
new3.88 KB

Attached is a patch that is most of the way there.

I've added an extra function in the api: geo_detect, which can successfully detect between all currently supported formats (wkb, wkt, and array).

We now detect the format before saving the field, and make sure we save the field in WKB. There is an additional bug that I found when creating this patch: the wkb that is selected from the database is not acceptable for insertion into the database. I've created a temporary work-around that converts to wkt and back again to get the wkb to behave properly.

gagarine’s picture

Status: Active » Reviewed & tested by the community

I try this patch with success. You think is better to commit and open a new issue for the problem with the WKB format from database?
I'm for a commit because this bug cause a tonne of problems with others modules like rules.

gagarine’s picture

Title: node_save does not save geo feilds » node_save does not save geo fields
gagarine’s picture

Priority: Critical » Normal
Status: Needs review » Needs work
StatusFileSize
new332 bytes

Just a small error. See the patch.

Note: you can NOT use this patch directly, first patch with #3

gagarine’s picture

Priority: Normal » Critical
Status: Reviewed & tested by the community » Needs work

#666544: Data Loss on Node Save When No Permissions marked as duplicate

Ok I think we can pass this issue to "critical", people without permission on the field can erase content...

solipsist’s picture

Tested and appears to fix the problem.

A simple piece of PHP such as this will trigger the bug:

<?php
$res = db_query('SELECT nid FROM node WHERE type ="location" limit 0,1000');
while ($row = db_fetch_object($res)) {
  $node = node_load($row->nid);
  node_save($node);
}
?>
gagarine’s picture

Status: Needs work » Needs review

You review the patch #6 with #3?

solipsist’s picture

I applied both patches (#3 and #6 in that order) and it seems to fix the problem.

gagarine’s picture

Priority: Normal » Critical
Status: Needs work » Reviewed & tested by the community

Thanks! I use the both patch in production for almost 2 months without problem in different context.

Marked as reviewed. We can certainly have a cleaner solution... but this one seem to work.

strk’s picture

The patch doesn't apply cleanly against CVS HEAD.
Is it still needed there ?

gagarine’s picture

@strk I don't know..

Try

$node = node_load($nid);
node_save($node);

if you don't lose any informations... it's nice we can close the bug.

ahtih’s picture

Version: » 6.x-1.x-dev

Apparently the patch is still needed in CVS HEAD, since node_load()+node_save() sequence still clobbers all geo fields in node.

ahtih’s picture

StatusFileSize
new1.71 KB

Here is an alternative (simpler) patch solving the same node_load()+node_save() problem. It is against current CVS HEAD. As opposed to the patch in #3, it does not try to detect the format of 'geo' attribute, but instead relies on the fact that geo_field('load', ...) puts a 'wkb' attribute in field item in addition to the usual 'geo' attribute. I'm not sure which of the patches is better, I'm using this one, but your mileage may vary.

KhaledBlah’s picture

the patch in #3 and #6 does solve the node_load() + node_save() problem. However, in conjunction with the rules module (see: #574894: Rules triger "After updating existing content" erase Geo field) it does not work. While the saved value's is a WKT syntactically the floating point numbers are messed up. I am trying to understand the problem but I'd grateful for any hints.