%let name=impact;
filename odsout '.';


/*
Copy-n-pasted impact data from the following web table...
http://www.solarviews.com/eng/crater.htm
Into the following excel spreadsheet...
impact.xls
Then edited Mjolnir Norway and changed "73°48N" to "73°48'N" (ie, added the single-quote, for minutes)
*/
PROC IMPORT OUT=impact_sites DATAFILE="impact.xls" DBMS=XLS REPLACE;
 GETNAMES=YES;
 MIXED=YES;
RUN;

/* Sort them so the biggest (most important) dots get printed on top, so they're visible */
proc sort data=impact_sites out=impact_sites;
by diameter__km_;
run;

/* Parse out the lat/long, and get them into decimal format */
data impact_sites; set impact_sites;

latdeg=0; latdeg=scan(latitude,1,'°');
latmin=0; latmin=scan(scan(latitude,2,'°'),1,"'");
latdir=trim(left(scan(latitude,2,"'")));

longdeg=0; longdeg=scan(longitude,1,'°');
longmin=0; longmin=scan(scan(longitude,2,'°'),1,"'");
longdir=trim(left(scan(longitude,2,"'")));

lat= (latdeg + (latmin/60)); 
if latdir eq 'S' then lat=lat*-1;

long=(longdeg + (longmin/60));    
if longdir eq 'W' then long=long*-1;

run;


/* Create the annotated colored markers */
data impact_sites; set impact_sites;
length function style $ 12 color $ 8 position $ 1 text $ 20;
xsys='2'; ysys='2'; hsys='3'; when='a';
anno_flag=1;

if diameter__km_ >= 100 then do;
 color='cxa50f15';
 zoom=9;
 end;
else if diameter__km_ >= 50 then do;
 color='cxde2d26';
 zoom=10;
 end;
else if diameter__km_ >= 10 then do;
 color='cxfb6a4a';
 zoom=11;
 end;
else if diameter__km_ >= 1 then do;
 color='cxfcae91';
 zoom=12;
 end;
else do;
 color='cxfee5d9'; 
 zoom=14;
 end;

function='pie'; rotate=360; size=.5; position='5'; 
style='psolid'; output;
style='pempty'; color='black'; output;
run;

data world; set mapsgfk.world (where=(density<=2 and idname^='Antarctica') drop=resolution);
run;

/* combine, project, and separate */
data combined; set world impact_sites; run;
proc gproject data=combined out=combined dupok latlong eastlong degrees project=gall;
  id id;
run;
data world impact_sites; set combined;
if anno_flag=1 then output impact_sites;
else output world;
run;

/* Assign these long strings *after* the gproject - this speeds up the gproject considerably! */
data impact_sites; set impact_sites;
/* This same url will be used in the 'proc gmap' drilldown, and the table */
length google_loc google_general $300;

google_loc='http://maps.google.com/?ie=UTF8&ll='||trim(left(lat))||','||trim(left(long))||
 '&spn=0.074899,0.102654&t=h&z='||trim(left(zoom))||'&om=1';

google_general='https://www.google.com/search?q=impact+crater+'||trim(left(name));

/* This is the html title= charttip and href= drilldown for the annotated circle */
length html $1024;
if style='pempty' then html=
 'title='||quote(
  trim(left(name))||'0d'x||
  'Diameter: '||trim(left(diameter__km_))||' km'||'0d'x||
  'Age: '||trim(left(age__ma_))||' Ma')||
 ' href='||quote(google_loc);

run;


goptions device=png;
goptions xpixels=1000 ypixels=650;
goptions border;
 
ODS LISTING CLOSE;
ODS HTML path=odsout body="&name..htm" 
 (title="SAS/Graph Meteorite Impact Crater Map") style=htmlblue;

goptions htitle=5pct ftitle="albany amt/bold" htext=2.75pct ftext="albany amt" ctext=gray;

title1 link="impact_info.htm"
 ls=1.0 justify=left move=(+2,+0) "Meteorite Impact Craters";
/* footnote "click dots for detailed satellite map"; */
/* use a note, rather than footnote, so it will share the space with the map */

pattern1 color=lig;

proc gmap data=world map=world anno=impact_sites; 
note move=(50,3)pct "click dots for detailed satellite map";
id id; 
choro segment / levels=1 nolegend 
  coutline=graybb 
  des='' name="&name"; 
run;

/* Now, create the table, with links */

/* The table needs the drilldowns formatted a little differently than proc gmap ... */
proc sql noprint;
create table table_data as 
select unique name, google_loc, google_general, latitude, longitude, diameter__km_, age__ma_
from impact_sites
order by diameter__km_ descending;
quit; run;

/* set up the drilldown links for the table text */
data table_data; set table_data;
 length link1 link2 link3 $300 href_general href_loc $300;
 label link1='Impact Site';
 label link2='Latitude';
 label link3='Longitude';
 label diameter__km_='Diameter (km)';
 format diameter__km_ comma10.3;
 label age__ma_='Age (ma)';
 href_general='href='||quote(trim(left(google_general)));
 href_loc='href='||quote(trim(left(google_loc)));
 link1 = '<a ' || trim(href_general) || ' target="body">' || htmlencode(trim(name)) || '</a>';
 link2 = '<a ' || trim(href_loc) || ' target="body">' || htmlencode(trim(latitude)) || '</a>';
 link3 = '<a ' || trim(href_loc) || ' target="body">' || htmlencode(trim(longitude)) || '</a>';
run;

title1 "Meteorite Impact Craters";

footnote1 link="http://www.solarviews.com/eng/crater.htm" 
 "Based on 10sep2007 data from http://www.solarviews.com/eng/crater.htm ";

proc print data=table_data noobs label;
var link1 link2 link3 diameter__km_ age__ma_;
run;

quit;
ODS HTML CLOSE;
ODS LISTING;
