diff --git a/geocoder/ip-addresses/README.md b/geocoder/ip-addresses/README.md new file mode 100644 index 0000000..5702d3c --- /dev/null +++ b/geocoder/ip-addresses/README.md @@ -0,0 +1,16 @@ +IP address geocoder +=============== + +### Function + +### Creation steps + +1. upload a new dataset to the geocoder table, call it latest_ip_address_locations +2. Run the sql/build_data_table script to update the table + +### Data Sources + +### Preparation details + + + diff --git a/geocoder/ip-addresses/sql/build_data_table.sql b/geocoder/ip-addresses/sql/build_data_table.sql new file mode 100644 index 0000000..dbf5dc3 --- /dev/null +++ b/geocoder/ip-addresses/sql/build_data_table.sql @@ -0,0 +1,9 @@ + +---- Postal Code Polygon table --- +--- --- + +-- Clear table + +DELETE FROM ip_address_locations; +INSERT INTO ip_address_locations (the_geom, network_start_ip) SELECT the_geom, network_start_ip::inet FROM latest_ip_address_locations; +DROP TABLE latest_ip_address_locations; diff --git a/geocoder/ip-addresses/sql/geocoder.sql b/geocoder/ip-addresses/sql/geocoder.sql new file mode 100644 index 0000000..3935cb5 --- /dev/null +++ b/geocoder/ip-addresses/sql/geocoder.sql @@ -0,0 +1,31 @@ +CREATE OR REPLACE FUNCTION geocode_ip(ip text[]) + RETURNS SETOF geocode_ip_v1 AS $$ + DECLARE + ret geocode_ip_v1%rowtype; + n TEXT; + new_ips INET[]; + old_ips TEXT[]; + BEGIN + FOR n IN SELECT unnest(ip) LOOP + BEGIN + IF family(n::inet)=6 THEN + new_ips := array_append(new_ips, n::inet); + old_ips := array_append(old_ips, n); + ELSE + new_ips := array_append(new_ips, ('::ffff:'||n)::inet); + old_ips := array_append(old_ips, n); + END IF; + EXCEPTION WHEN OTHERS THEN + SELECT n AS q, NULL as geom, FALSE as success INTO ret; + RETURN NEXT ret; + END; + END LOOP; + FOR ret IN WITH ips AS (SELECT unnest(old_ips) s, unnest(new_ips) net), + matches AS (SELECT s, (SELECT the_geom FROM ip_address_locations WHERE network_start_ip <= ips.net ORDER BY network_start_ip DESC LIMIT 1) geom FROM ips) + SELECT s, geom, CASE WHEN geom IS NULL THEN FALSE ELSE TRUE END AS success FROM matches + LOOP + RETURN NEXT ret; + END LOOP; + RETURN; +END +$$ LANGUAGE 'plpgsql' SECURITY DEFINER; \ No newline at end of file diff --git a/geocoder/postal-codes/README.md b/geocoder/postal-codes/README.md index 209ad88..71ff354 100644 --- a/geocoder/postal-codes/README.md +++ b/geocoder/postal-codes/README.md @@ -17,13 +17,3 @@ French polygons - http://www.data.gouv.fr/dataset/fond-de-carte-des-codes-postau ### Preparation details - -## Admin0_synonyms - - -### Ranks - -| rank number | origin data | origin column | description | -|-------------|-----------------------------|---------------|----------------------| - -__notes:__ \ No newline at end of file