Hi all,
What is the best way to convert two existing fields (lat / lon) into a Geofield?

I've got an existing content type with more than 1000 record.
This content type has two fields latitude and longitude, i would like to convert these into a new geofield field.

Else, I could export all records into a csv file and re-import all with the feeds module, but how is the correct mapping?

Thanks to all for the help

Comments

jjchinquist’s picture

If you want to do it programmatically, this may help (I am making some assumptions here though):
Add a GeoField instance to the node/entity that is a point.
Programmatically load the node and then update:

$d7_entity = node_load(...);
$d7_entity->field_geofield['und'][0] = array(
  'wkt' => 'POINT (' . $d7_entity->field_longitude['und'][0]['value'] . ' ' . $d7_entity->field_latitude['und'][0]['value'] . ')',
  'geo_type' => 'point',
  'lat' => $d7_entity->field_latitude['und'][0]['value'],
  'lon' => $d7_entity->field_longitude['und'][0]['value'],
);
node_save($d7_entity);
nassiesse’s picture

Great!
Thanks for help!

With this script I updated all records.
I hope this is useful to other users!


<?php
define('DRUPAL_ROOT', getcwd());
$_SERVER['REMOTE_ADDR'] = "localhost"; // Necessary if running from command line
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$nodes = node_load_multiple(array(), array('type' => 'location'));

$length = count($nodes);
echo "TOT:".$length."\n";

foreach($nodes as $node) {

	if (isset($node->field_latitudine['und'])) {
		$node->field_location_coord['und'][0] = array(
		'wkt' => 'POINT (' . $node->field_longitudine['und'][0]['value'] . ' ' . $node->field_latitudine['und'][0]['value'] . ')',
		'geo_type' => 'point',
		'lat' => $node->field_latitudine['und'][0]['value'],
		'lon' => $node->field_longitudine['und'][0]['value'],
		);
		node_save($node);
	}
  
}

echo "Fine!";

?>

Brandonian’s picture

Status: Active » Fixed

Marking as fixed.

Status: Fixed » Closed (fixed)

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