Geocoder trans-codes other fields to longitude/latitude information. It would be great to have an option to compute an Address Field from another longitude/latitude field. A kind of inverse geocoding, to geocode an Address from a Geofield. Would it be possible?

Comments

phayes’s picture

Title: Geocode Addresses from Geofields » Reverse-Geocode support
phayes’s picture

By the way, have you checked out GeoField's "address" formatter?

cesareaugusto’s picture

By the way, have you checked out GeoField's "address" formatter?

I think I haven't such a formatter. The only display formatters I got available for my geofields are

  • Well Known Text
  • GeoJSON
  • KML
  • GPX
  • Latitude/longitude
  • Latitude only
  • Longitude only
  • Geometry type
  • Openlayers
  • Definition list (accessibility)
  • Descriptive text (accessibility)

No address formatter seems to be here :(

cesareaugusto’s picture

Issue summary: View changes

spell check error

phayes’s picture

Ah. I think it's hidden away in "Descriptive text (accessibility)". Likely not exactly what you want.

phayes’s picture

So this is the way I would like this handled:

1. A new function called geocoder_reverse that is analogous to the "geocoder" function. It will take a geoPHP object and return a string (or whatever)
2. Re-write geocoder_get_hanlders so that you can specify the "type" 'forward' or 'reverse' or 'both' -- defaults to both.
3. Handlers should define 3 new functions: reverse_callback, reverse_field_types, and reverse_field_callback to be "reverse" enabled. Some modules might ONLY define these so as to make them ONLY reversers....
4. Define a new widget geocoder_reverse, and follow similar patterns as the forward geocoder

cesareaugusto’s picture

From a user's perspective I would immagine it as the very inverse of geocode from an address field function. Given we already got a geofield type field, when creating a new address type field we should have the option geocode from a geofield.

cesareaugusto’s picture

Any news on the reverse geocoding thing? :)

phayes’s picture

Status: Active » Postponed

@cesaragusto,

I don't have the time to work on this. I am, however, willing to review patches.

cesareaugusto’s picture

@phayes I am not a coder, but I would be glad to help somehow... perhaps testing or doing the UI thing...

giorgio79’s picture

Additional feature idea: If the user submits a partial address such as

USA
Dallas
75932
XY street

State is missing (TX), but Google will still geocode it. So, after a successful gecodoe, do a reverse geocode and fill in the blanks, so the user would be offered with:

USA
TX
Dallas
75932
XY street

cesareaugusto’s picture

Additional feature idea: If the user submits a partial address such as

USA
Dallas
75932
XY street

State is missing (TX), but Google will still geocode it. So, after a successful gecodoe, do a reverse geocode and fill in the blanks, so the user would be offered with:

USA
TX
Dallas
75932
XY street

That would be pretty useful when it comes about zip codes of larges cities. They commonly have more than one zip code according to the zone we refer to. Users usually know the city main zip code, but not everyone knows the zip code of the current area.

giorgio79’s picture

cesareaugusto’s picture

@giorgio79: i'm not much into the sandbox thing... would it be possible to release a standard archive as every module does? perhaps at least a first dev one...

shushu’s picture

Status: Postponed » Needs review
StatusFileSize
new4.73 KB

Hi all,
Since I needed reverse geocoding and found out the module and this issue, I decided to create the patch.
I actually used @phayes comment #5 as a nice guidelines, with some changes to make it fit to the use of ctools plugin format.

It works nicely for me, via another rules integration module I created to use it (hope to upload soon).

I will be happy to finetune as needed.

cesareaugusto’s picture

@shushu: I would like to try out your patch! To which version of the module should the patch be applied to?

cesareaugusto’s picture

@shushu: I applied the patch against the 1.2 (stable) version. I upgraded. Though I cannot find the "Reverse-geocode from another field" form element to edit the data of postal address fields. How to use it?

shushu’s picture

Sorry if I wasn't clear.
I used the API via a new Rules module I made - http://drupal.org/project/geocoder_rules
Hope this helps,
Shushu

socialnicheguru’s picture

I am unclear.

the patch in #14 is against geocoder
what is geocoder_rules needed for?

thanks for explaining.

shushu’s picture

While Geocoder can be used directly wth some modules via their fields, it is mainly an API module.
As such, and in order to create the most flexible solution (as I see it), I created some Rules actions that uses the Geocoder as API calls.
I am not against developing more support for reverse geocoding directly with the fields, but since my needs focus on Rules integration, this was my solution.
So,
- The reverse geocoding works as API, and can be used directly.
- Using the Rules module you can call the API via actions.
- If you feel this works, but support without Rules is needed, I will try to find time and make it happen as well
Regards,
Shushu

cesareaugusto’s picture

@shushu: Would it be hard to implement the reverse geocoding thing as the very inverse of the Geocoder? I mean having the possibility to choose "reverse geocoding" within the address fields setting (as geocoder does for geofields).

shushu’s picture

It shouldn't be hard, but I don't have the spare time to make it happen.
It shouldn't be hard also to install Rules, the geocoder rules, and to make it happen without any additional coding right away. This will also allow you to learn Rules, which is a great and powerful module...

sylvaticus’s picture

@shushu: hi.. could you explain a little bit how to use your patch?
In particular, what does the following fields mean in the action settings?

  • Handler
  • Language
  • Address
  • Accuracy

I think I got that "Address" and "Accuracy" are output variables that we can then use in the remaining of the rule chain, and "Language" maybe is the language we want to use when querying google ?? But "Handler" what is it?

I have a geofield with lat/lon, can I use the rule action to get only the country or would I get the whole address ?

sylvaticus’s picture

think I got, and it's working :-)
Handling is just the word "google" :-)

sylvaticus’s picture

For those interested, this is a D7 code snippet that uses the geocoder api as patched in #14 to reverse geocode a geofield (with embedded lat/lon coordinated) and put the address formatted in a computed field.
In this case I put just the country, but you can play with the $address object to get what you need (it uses computed_field rather than rules or geocoder_rules).

$country = "Not Defined";
if(isset($entity->field_location['und'][0])){
  $lat=$entity->field_location['und'][0]['lat'];
  $long=$entity->field_location['und'][0]['lon'];
  $options = array();
  $options['language'] = "en";
  $address = geocoder_reverse("google", $lat, $long, $options);
  //dpm($address);
  if($address){
    //watchdog("DEBUG REVERSE GEOCODING", var_export($address, true));
    foreach ($address->data['geocoder_address_components'] as $component){
      if ($component->types[0] == 'country'){
        $country = $component->long_name;
        break;
      }
    }
  }
}
$entity_field[0]['value'] = $country;
sylvaticus’s picture

Status: Needs review » Reviewed & tested by the community
cesareaugusto’s picture

@sylvaticus: Sorry for the dummy question.... though........ How to use your code (from a newbie perspective)? Is it a patch against the module?

steinmb’s picture

Status: Reviewed & tested by the community » Needs work

@sylvaticus Could you pls. provide a patch containing #14 and your code from #24?

sylvaticus’s picture

The code in #24 is just a (working) example of USAGE of the geocoder module as patched in #14.
Application case: in a comtent type you have a field with lat/long (type:geofield) and you want to save on an other field the corresponding country (or any other address elements).
Steps:
- download geocoder and patch it with #14
- download computed_field, a module that let you define the value of a field from php code (so, the user doesn't write or even see this field on editing a node, it is computed automatically);
- add to the content type a computed field and in its edit form insert the code in #24
- edit the code in #24 according to your needs (e.g. yr language instead of "und")
- now whenever a node is saved the computed field read the data in the geofield and uses the reverse feocoding feature provided by #14 to get the country.

shushu’s picture

@steinmb, my way to implement my patch is using Geocoder Rules module I created, and a simple Rule example below:

{ "rules_geocode_location" : {
    "LABEL" : "geocode location",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "geocoder_rules", "php" ],
    "ON" : [ "node_presave" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_location" } },
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_location_name" } }
    ],
    "DO" : [
      { "geocoder_rules_reverse_geocode" : {
          "USING" : {
            "handler" : "google",
            "latitude" : [ "node:field-location:lat" ],
            "longitude" : [ "node:field-location:lon" ],
            "language" : "iw"
          },
          "PROVIDE" : {
            "address" : { "address" : "Address" },
            "accuracy" : { "accuracy2" : "Accuracy" }
          }
        }
      },
      { "drupal_message" : { "message" : "Address=\u003C?php echo $address; ?\u003E, accuracy=\u003C?php echo $accuracy2;?\u003E" } },
      { "data_set" : { "data" : [ "node:field-location-name" ], "value" : [ "address" ] } }
    ]
  }
}

What this rule do:

  • Take lat/long from GeoField field
  • Call the reverse geocoding with it (note I use "hardcoded" iw language, change it to your needs)
  • Set the value into a standard text field

Hope this helps,
Shushu

cesareaugusto’s picture

Just discussing about user-friendliness and coherence with the geocoder module behavior...... wouldn't it be easier to the "average user" to have a "reversegeocode from an geofield" option in the address field?

shushu’s picture

You are right, making proper support for address field should be created.
As for myself, I don't use the addressfield at all, and I don't have spare time to create this support.
I will be happy to help anyone who will try to develop it.

Robin_K’s picture

It might be worth noting that this functionality is currently in GeoPHP, geoPHP::load($geoWKT,'wkt')->out('google_geocode') returns the address of $geoWKT.
This works with all the formats supported by GeoPHP.

Robin_K’s picture

Issue summary: View changes

sorry another spell mistake

mastermindg’s picture

Thanks Robin_K

I spent a whole lot of time tracking this point down. It's important to mention that from a mobile device I'm only able to grab long/lat, so for a more friendly presentation this needs to be reverse-geocoded. I used the following code (along with smart_ip and geophp) to accomplish this:

$longitude = $_SESSION['smart_ip']['location']['longitude'];
$latitude = $_SESSION['smart_ip']['location']['latitude'];
geophp_load();
$json = '{
    "type": "Point",
    "coordinates": [' . $longitude . ',' . $latitude . ']
}';
$myaddress = geoPHP::load($json,'json')->out('google_geocode');
drupal_set_message($myadress);
AgentJay’s picture

thanks for the sample code,
I have installed the geophp module and the code works perfectly, but I only want the country value returned, is there an easy way to do this?

mastermindg’s picture

I figured out (after the previous post) that you can output the geocoded results as an array:

$mypoint = geoPHP::load($json,'json')->out('google_geocode','array');

$mypoint is now an array and Country can be selected like so:

$user_country = $mypoint[5]->short_name;

socialnicheguru’s picture

Issue summary: View changes

Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in geocoder_reverse() (line 395 of /var/aegir/platforms/7/modules/all/geocoder/geocoder.module

$processor = ctools_get_plugins('geocoder', 'geocoder_reverse_handler', $handle\r);
  $address = call_user_func($processor['callback'], $lat, $long, $options);   <- 395
syawillim’s picture

Is it possible to reverse geocode from an attached image EXIF data? I'd like to categorize nodes by country if someone could help out with an example of the rule.

jelle_s’s picture

Status: Needs work » Needs review

This patch was set to NW in #27, while it really shouldn't have been. Is there any reason why this isn't yet committed? Is something missing from this patch? It would be an awesome feature! It seems like I have a similar use case as #37, no doubt others will have use cases as well!

jgullstr’s picture

The patch in #14 works nicely for me. As this functionality is not interfering with geocoders existing features, I think this can be commited -> RTBC.

I've created an addressfield widget for reverse-geocoding geofields into addressfield values by using the functionality provided by this patch: Addressfield reverse geocode widget. A similar approach to how this module works could be used to create addresses from EXIF data (@syawillim).

jgullstr’s picture

Status: Needs review » Reviewed & tested by the community
Kostasm83’s picture

Hi, I tried a lot both of #29 and #28 ways but I didn't have any result.
In #29 way I have these errors:

Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in geocoder_reverse() (line 32 of /var/www/katastima/sites/all/modules/geocoder/geocoder.module).
Notice: Trying to get property of non-object in geocoder_rules_reverse_geocode() (line 23 of /var/www/katastima/sites/all/modules/geocoder_rules/geocoder_rules.module).
Notice: Trying to get property of non-object in geocoder_rules_reverse_geocode() (line 24 of /var/www/katastima/sites/all/modules/geocoder_rules/geocoder_rules.module).

Maybe i do something wrong... can You help pz...
Thank You.

zwerg’s picture

Trying this rules code:

{ "rules_geocode_location" : {
    "LABEL" : "geocode location",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "geocoder_rules", "php" ],
    "ON" : [ "node_presave" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_location" } },
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_location_name" } }
    ],
    "DO" : [
      { "geocoder_rules_reverse_geocode" : {
          "USING" : {
            "handler" : "google",
            "latitude" : [ "node:field-location:lat" ],
            "longitude" : [ "node:field-location:lon" ],
            "language" : "iw"
          },
          "PROVIDE" : {
            "address" : { "address" : "Address" },
            "accuracy" : { "accuracy2" : "Accuracy" }
          }
        }
      },
      { "drupal_message" : { "message" : "Address=\u003C?php echo $address; ?\u003E, accuracy=\u003C?php echo $accuracy2;?\u003E" } },
      { "data_set" : { "data" : [ "node:field-location-name" ], "value" : [ "address" ] } }
    ]
  }
}

I'll get the following error message:

Integrity check for the imported configuration failed. Error message: The data type of the configured argument does not match the parameter's value requirement..

Rules does not accept the value of data_set" : { "data" : [ "node:field-location-name" ], "value" : [ "address" ]

Is there any possibility to solve this?

xaa’s picture

I can't apply the patch on geocder-7.x-1.x-dev, any clue please ?

fatal: corrupt patch at line 6

steinmb’s picture

Your right. The patch cannot be applied with git. Here is a re-roll that should work.

pol’s picture

Hi all,

I'm working on the version 2.x of Geocoder, it's now based on Geocoder PHP library, which support geocoding and reverse geocoding out of the box.

You can find the repository here: https://github.com/drupol/geocoder

It's still in heavy development but it's getting along pretty nicely.

Let me know what you think.

Thanks.

haggins’s picture

Here's a reroll against current 7.x-1.3

@Pol
Unless version 2.x isn't stable it would be great to have this patch committed for all that depend on 1.x, like us ;)

jgullstr’s picture

@Pol
Unless version 2.x isn't stable it would be great to have this patch committed for all that depend on 1.x, like us ;)

Second this.

DerTobi75’s picture

Unless version 2.x isn't stable it would be great to have this patch committed for all that depend on 1.x, like us ;)

Third that!

makbuk’s picture

# 46 Works for me. Thanks.

  • Pol committed 5701107 on 7.x-1.x authored by haggins
    Issue #1461164 by shushu, steinmb, haggins: Reverse-Geocode support
    
pol’s picture

Thanks !

pol’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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