SELECT util.search_path_append('util');

-- runtime: 40 s ("real 0m21.770s" + "Time: 17304.769 ms") @starscream
SELECT set_col_types(:table_str, ARRAY[
  ('decimalLatitude'                , 'double precision')
, ('decimalLongitude'               , 'double precision')
, ('latLongDomainValid_verbatim'    , 'integer')
, ('latLongInCountry_verbatim'      , 'integer')
, ('latLongInStateProvince_verbatim', 'integer')
, ('latLongInCounty_verbatim'       , 'integer')
]::col_cast[]);

ALTER TABLE :table ALTER COLUMN "decimalLatitude"  SET NOT NULL;
ALTER TABLE :table ALTER COLUMN "decimalLongitude" SET NOT NULL;

-- runtime: 55 s ("real 0m53.677s")
SELECT create_if_not_exists($$CREATE INDEX geoscrub_output_input ON geoscrub_output ("decimalLatitude", "decimalLongitude", country, "stateProvince", county)$$);


-- map_*() derived cols
-- runtime: 2 min @starscream

CREATE OR REPLACE FUNCTION map_coord_validity(value integer)
  RETURNS boolean AS
$BODY$
/*
"For latlonvalidity:
-1: Latitude and/or longitude is null
 0: Coordinate is not a valid geographic location
 1: Coordinate is a valid geographic location
" (http://vegpath.org/fs/inputs/.geoscrub/_src/README.TXT)
*/
SELECT util._map('-1=>NULL, 0=>false, 1=>true', $1::text)::boolean
$BODY$
  LANGUAGE sql IMMUTABLE
  COST 100;

SELECT mk_derived_col((:table_str, 'latLongDomainValid'), $$map_coord_validity("latLongDomainValid_verbatim")$$); -- runtime: 25 s ("Time: 26708.932 ms") @starscream

CREATE OR REPLACE FUNCTION map_geovalidity(value integer)
  RETURNS boolean AS
$BODY$
/*
"For countryvalidity/stateprovincevalidity/countyvalidity:
-1: Name is null at this or some higher level
 0: Complete name provided, but couldn't be scrubbed to GADM
 1: Point is >5km from putative GADM polygon
 2: Point is <=5km from putative GADM polygon, but still outside it
 3: Point is in (or on border of) putative GADM polygon
" (http://vegpath.org/fs/inputs/.geoscrub/_src/README.TXT)
*/
SELECT util._map('-1=>NULL, 0=>NULL, 1=>false, 2=>true, 3=>true', $1::text)::boolean
$BODY$
  LANGUAGE sql IMMUTABLE
  COST 100;

SELECT mk_derived_col((:table_str, 'latLongInCountry'      ), $$map_geovalidity("latLongInCountry_verbatim"      )$$); -- runtime: 25 s ("Time: 27884.129 ms") @starscream
SELECT mk_derived_col((:table_str, 'latLongInStateProvince'), $$map_geovalidity("latLongInStateProvince_verbatim")$$); -- runtime: 25 s ("Time: 27228.239 ms") @starscream
SELECT mk_derived_col((:table_str, 'latLongInCounty'       ), $$map_geovalidity("latLongInCounty_verbatim"       )$$); -- runtime: 25 s ("Time: 27337.520 ms") @starscream
