PK (Point Kilométriques en France)
Posted by DF45 on 14 October 2016 in French (Français). Last updated on 29 September 2026.L’Opendata officiel a mis en ligne la liste des points kilométriques en France.
https://www.data.gouv.fr/fr/datasets/bornage-du-reseau-routier-national/
L’info m’est arrivée via le blog … d’OSM Maroc ! Un premier import dans QGIS montre les routes et nationales à deux voies. C’est encourageant. Une analyse plus détaillée indique que c’est plus … varié. Beaucoup de routes on un PK gauche et un PK droite … mais pas tous. Certaines ont un seul PK de type I (indéterminé ?) ou G ou D. D’autres PK n’ont carrément pas de coordonnées. Après un test de correction dans un tableur, on va se tourner vers un import dans PostGreSQL afin de pouvoir rejourer les corrections d’élimination des PK incorrects et de faire une moyenne Gauche/Droite.
Le code ci-dessous ;
– ********* Transform_PK.sql ***************
– *
– * GOAL : Transform the french kilometer point file from french main roads found in
– * opendata.gouv.fr in order to have only one PK instread of having a left and right one
– * Include cleanup from obviously errors (lat/long = zero)
– *
– * PREREQUISITE : a PostGres instance with postgres account
– *
– * parametres : to be launched with PSQL … -v ficin=’c:...’ -v ficout=’c:...’
– * ficin = complete name (with directory) of the original files
– * ficout = complete name (with directory) of the output file
– * launching command : (for a postgres DB)
– * psql -Upostgres –file=transform_PK.sql -q –log-file=transform_PK.log -v ficin=’xxx’ -v ficout=’xxx’-t
– *
– * 2016/12/09 Creation DF45
– ******************************
– ** Preparation (table drop/creation) drop table borne; drop table borne_new; create table borne (route char(10), pr char(3), deppr char(3), concessionPr char(1), abs char(1), x_temp char(10), y_temp char(10), z_temp char(10), cote char(1));
– *** Import of original files COPY borne FROM :ficin DELIMITER ‘ ‘ CSV HEADER ;
– *** conversions of commas to point alter table borne add column x double precision; alter table borne add column y double precision; alter table borne add column z double precision; update borne set x=to_number(x_temp,’9999999D99’); update borne set y=to_number(y_temp,’9999999D99’); update borne set z=to_number(z_temp,’9999D99’);
– *** removal of PK without coordinates create table borne_new as select * from borne where 1=2; create unique index on borne_new (route,pr,deppr,concessionPr); delete from borne where x is null; delete from borne where y is null;
– ** Insertion of PK of type I insert into borne_new (select * from borne where cote=’I’); delete from borne where cote=’I’; – select count() from borne where cote=’D’; – select count() from borne where cote=’G’; – select count() from borne where route||concessionPr||’-‘||pr in (select route||concessionPr||’-‘||pr from ((select route||concessionPr||’-‘||pr from borne where cote=’D’) intersect (select route||concessionPr||’-‘||pr from borne where cote=’G’)) resultat);
– *** insertion of lonely PK insert into borne_new (select * from borne where route||’-‘||pr in (select cod from (select route||’-‘||pr cod from borne where cote=’D’ except select route||’-‘||pr cod from borne where cote=’G’) t)); insert into borne_new (select * from borne where route||’-‘||pr in (select cod from (select route||’-‘||pr cod from borne where cote=’G’ except select route||’-‘||pr cod from borne where cote=’D’) t)); delete from borne where route||’-‘||pr in (select cod from (select route||’-‘||pr cod from borne where cote=’D’ except select route||’-‘||pr cod from borne where cote=’G’) t); delete from borne where route||’-‘||pr in (select cod from (select route||’-‘||pr cod from borne where cote=’G’ except select route||’-‘||pr cod from borne where cote=’D’) t);
– *** Calculation of mean coordinates of “left” and “Right” PK as they are expected insert into borne_new (pr, route, deppr, concessionPr, abs, x, y, z, cote) select a.pr, a.route, a.deppr, a.concessionPr, a.abs, (a.x+b.x)/2, (a.y+b.y)/2, (a.z+b.z)/2,’M’ from borne a, borne b where a.pr=b.pr and a.route=b.route and a.deppr=b.deppr and a.concessionPr=b.concessionPr and b.cote=’D’ and a.cote=’G’;
– *** New file creation copy (select rtrim(route,’ ‘) “route”,rtrim(pr,’ ‘) “pr”,rtrim(deppr,’ ‘) “depPr”, concessionpr “concessionPr”, abs, to_char(x,’9999999D99’) “x”, to_char(y,’9999999D99’) “y”, z, cote from borne_new order by deppr, route, pr, concessionPr) to :ficout with delimiter ‘ ‘ CSV header;
– *** cleaning of temporary tables drop table borne; drop table borne_new;
Discussion
Comment from marczoutendijk on 18 October 2016 at 12:50
Now that it works, you should come up with something more interesting for the other mappers reading here!