Unfortunately, OGR sees the KML file as having 247 layers (ogrinfo doc.kml
), which is why you see the mess.
I would do a direct KML -> PostGIS conversion. You can keep a clean PostGIS DB by using a schema to contain the mess temporarily. From your SQL:
CREATE SCHEMA import;
Now from your shell (I'm using OSGeo4W Shell):
$ ogr2ogr -append -f PostgreSQL PG:"dbname='your_db' user='postgres' password='secret'" \
doc.kml -lco SCHEMA=import -lco EXTRACT_SCHEMA_FROM_LAYER_NAME=NO \
-lco OVERWRITE=YES -lco LAUNDER=NO
(No errors!)
Next, you need to stack your tables into one table in the public schema (assuming you have at least PostgreSQL 9.0 for the DO):
-- Create new table for all features
CREATE TABLE ice_rinks (gid serial PRIMARY KEY);
SELECT AddGeometryColumn('public', 'ice_rinks', 'geom', 4326, 'POINT', 3);
ALTER TABLE ice_rinks ADD COLUMN division text NOT NULL;
ALTER TABLE ice_rinks ADD COLUMN name text;
ALTER TABLE ice_rinks ADD COLUMN description text;
-- Combine them
DO $$DECLARE r record;
BEGIN
FOR r IN (SELECT relname, trim(relname) AS division
FROM pg_class c
JOIN pg_namespace n ON n.oid=c.relnamespace
WHERE n.nspname='import' AND relkind='r'
ORDER BY trim(relname))
LOOP
EXECUTE 'INSERT INTO ice_rinks(geom, division, "name", description) ' ||
'SELECT wkb_geometry, ' || quote_literal(r.division) || ', "Name", "Description" ' ||
'FROM import.' || quote_ident(r.relname) || ' ORDER BY ogc_fid';
END LOOP;
END$$;
I have 1265 features .. hopefully they're all there.
Lastly, cleanup. You can remove your import schema if the data looks good:
DROP SCHEMA import CASCADE;
And if you are using PostGIS 1.5 or prior:
DELETE FROM geometry_columns WHERE f_table_schema='import';
There are a few %%f too much in your command line. This one worked for me (with another conversion target):
for /R %%F IN ('D:\3.BATCH\FAUNE\*.tab') do C:\Users\Samy\Desktop\SIGMA\15.webmapping\ms4w\tools\gdal-ogr\ogr2ogr.exe -s_srs EPSG:3727 -t_srs EPSG:2975 -f "ESRI Shapefile" %%~dpnF_2975.shp %%F
Main obstacle is that %%F already contains the file extension, so you have to skip that with ~dpnF_2975.shp.
Note that f and F are considered as different by the Operating System.
You skipped the ~dpnf, but that is the essential part of clipping the file name to the desired part without extension. You can replace dp with full drive and path name.
Best Answer
On Windows, for the current and sub-directories under the current, try this command:
To briefly explain the trickery of what is going on here,
%~dpnf.tab
uses the variable %f, with which it adds the driver letter, path name (i.e., folder or directory), and extracts the file name (without the.shp
file extension). Lastly,.tab
is added immediately after the compound variable modifiers for the new extension.So if you are in directory
C:\MyData
, and you have data in this directory, and sub-directoriesC:\MyData\Region1
andC:\MyData\Region1\City1
, any Shapefile (with.shp
extension) will be processed, and a similar named file with.tab
will created in the same directory.