
上QQ阅读APP看书,第一时间看更新
Importing KML
As usual, we'll start with the dataset's metadata checkup:
ogrinfo 2.5_day_age.kml
The output shows that there is more than one layer:
INFO: Open of `2.5_day_age.kml'
using driver `LIBKML' successful.
1: Magnitude 5
2: Magnitude 4
3: Magnitude 3
4: Magnitude 2
Therefore, in order to review metadata for each layer at once, the following command should be used:
ogrinfo 2.5_day_age.kml -al -so
The output of the previous command is rather longish, so we'll truncate it a bit and only show the info for the first layer:
INFO: Open of `2.5_day_age.kml'
using driver `LIBKML' successful.
Layer name: Magnitude 5
Geometry: Unknown (any)
Feature Count: 2
Extent: (-101.000100, -36.056300) - (120.706400, 13.588200)
Layer SRS WKT:
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
TOWGS84[0,0,0,0,0,0,0],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9108"]],
AUTHORITY["EPSG","4326"]]
Name: String (0.0)
description: String (0.0)
timestamp: DateTime (0.0)
begin: DateTime (0.0)
end: DateTime (0.0)
altitudeMode: String (0.0)
tessellate: Integer (0.0)
extrude: Integer (0.0)
visibility: Integer (0.0)
drawOrder: Integer (0.0)
icon: String (0.0)
snippet: String (0.0)
This time, EPSG information is available, so we do not have to worry; ogr2ogr will create extra SRID definition in the database.
Once we've confirmed that this is the exact dataset we'd like to import, we can continue with the following command:
ogr2ogr -f "PostgreSQL" PG:"host=localhost port=5434 user=postgres dbname=mastering_postgis" 2.5_day_age.kml -nln data_import.usgs_earthquakes_kml -lco GEOMETRY_NAME=geom -append
Note the append param in the command earlier. This is required because our KML has more than one layer and ogr2ogr is importing them one by one. Without the append param, only the first layer would be imported and then ogr2ogr would fail with a similar output:
FAILED: Layer data_import.usgs_earthquakes_kml already exists, and -append not specified.
Consider using -append, or -overwrite.
ERROR 1: Terminating translation prematurely after failed
translation of layer Magnitude 4 (use -skipfailures to skip errors)
FAILED: Layer data_import.usgs_earthquakes_kml already exists, and -append not specified.
Consider using -append, or -overwrite.
ERROR 1: Terminating translation prematurely after failed
translation of layer Magnitude 4 (use -skipfailures to skip errors)
The cmd output should be similar to:
WARNING: Layer creation options ignored since an existing layer is
being appended to.
WARNING: Layer creation options ignored since an existing layer is
being appended to.
WARNING: Layer creation options ignored since an existing layer is
being appended to.
At this stage, the KML dataset should have made it to our PostGIS database.