BookmarkSubscribeRSS Feed
Dublin187
Calcite | Level 5
 

Hi,

 

I have a CSV file with a variable for longitude and another variable for latitude for over 20 000 observations.

 

I need to convert these coordinates into geohash by creating a new variable in SAS.

The end goal is to merge this data set with another one based with the identifier variable geohash.

 

Does anyone know how to do this? 

 

Thank you in advance for your help

4 REPLIES 4
s_lassen
Meteorite | Level 14

I could not find the routine for calculating geohashes. But you can get the result from geohash.org, e.g.:

data have; /* just som random test data */
  input latitude longitude;
cards;
45.63 114.67
18.876 11.44
;run;

filename geohash url;
data want;
  set have;
  fname=cats('http://geohash.org/?q=',latitude,',',longitude,'&format=url&redirect=0');
  infile geohash url filevar=fname truncover;
  input url $40.;  /* the result is like http://geohash.org/c216ne */
  geohash=scan(url,-1,'/');
run;
ChrisNZ
Tourmaline | Level 20

I replicated the logic shown in Wikipedia to transform a geohash into coordinates.

You can write the reverse operation.

You can do this in a macro or use proc fcmp to make it easier to call.

Does this help?

 

 

data _null_;
  GEOMAP_CHAR='ezs42';
  length GEOMAP_BIN LAT_BIN LON_BIN $50;
  do I=1 to length(GEOMAP_CHAR);
    VAL_DEC=index('0123456789bcdefghjkmnpqrstuvwxyz',char(GEOMAP_CHAR,I))-1;
    VAL_BIN=put(VAL_DEC,binary5.);     
    GEOMAP_BIN=catt(GEOMAP_BIN,VAL_BIN);      
  end;     
  do I=1 to length(GEOMAP_BIN);
    if mod(I,2)=0 then LAT_BIN=catt(LAT_BIN,char(GEOMAP_BIN,I));      
    else               LON_BIN=catt(LON_BIN,char(GEOMAP_BIN,I));      
  end;          
  do I=1 to length(LAT_BIN);
    SPAN=180/2**I;
    INC=ifn(char(LAT_BIN,I)='1',1,-1);
    LAT_INC=SPAN/2*INC;
    LAT+LAT_INC;       
  end;
  do I=1 to length(LON_BIN);
    SPAN=360/2**I;
    INC=ifn(char(LON_BIN,I)='1',1,-1);
    LON_INC=SPAN/2*INC;
    LON+LON_INC;       
  end;          
  putlog LAT= LON=;
run;

 

 

LAT=42.604980469 LON=-5.603027344

 

 

 

Dublin187
Calcite | Level 5

 

Hi,

Thank you for your suggestion. I am only a beginner in coding so writing the reverse operation of this code would very challenging for me. 

Can this translation program be applied for a longitude and latitude variables? or do I have to input the data individually?

 

ChrisNZ
Tourmaline | Level 20

This does the reverse translation:

data HAVE;
  LAT=42.604980469 ;
  LON=-5.603027344 ;
run;
data WANT;
  set HAVE;
  keep  GEOMAP_CHAR;
  GEOMAP_LEN=5;   
  length GEOMAP_CHAR $5 GEOMAP_BIN LAT_BIN LON_BIN $50;

  LOW=-180;
  do I=1 to ceil(GEOMAP_LEN*5/2+1);
    SPAN   = 360/2**I;  
    CENTER = LOW+SPAN; 
    INC    = (LON > CENTER);
    LOW    = LOW+SPAN*INC;               
    LON_BIN= catt(LON_BIN,INC);       
  end;          
  LOW=-90;
  do I=1 to ceil(GEOMAP_LEN*5/2);
    SPAN   = 180/2**I;  
    CENTER = LOW+SPAN; 
    INC    = (LAT > CENTER);
    LOW    = LOW+SPAN*INC;               
    LAT_BIN= catt(LAT_BIN,INC);       
  end;        
  do I=1 to ceil(GEOMAP_LEN*5/2);
    GEOMAP_BIN=catt(GEOMAP_BIN,char(LON_BIN,I));       
    GEOMAP_BIN=catt(GEOMAP_BIN,char(LAT_BIN,I));      
  end;          
  do I=1 to GEOMAP_LEN*5 by 5;  
    VAL_BIN=substr(GEOMAP_BIN,I,I+5);
    VAL_DEC=input(VAL_BIN,binary5.);
    VAL_CHAR=char('0123456789bcdefghjkmnpqrstuvwxyz',VAL_DEC+1); 
    GEOMAP_CHAR=catt(GEOMAP_CHAR,VAL_CHAR);
  end;
  putlog GEOMAP_CHAR=;
run;

GEOMAP_CHAR=ezs42

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 2220 views
  • 2 likes
  • 3 in conversation