# OGR DXF to KML conversion

I'm trying to convert a DXF 2010 file to KML using ogr2ogr:

ogr2ogr -f "KML" outfile.kml infile.dxf


In the file, \$INSUNITS is 6, that's meters, if I'm correct? The problem is that DXF is not georeferenced, and I get errors like:

ERROR 1: Latitude -1113854.310000 is invalid. Valid range is [-90,90]. This warning will not be issued any more
Warning 1: Longitude -684136.440000 has been modified to fit into range [-180,180]. This warning will not be issued any more


I'm pretty new to this stuff, and in the last weeks I haven't found documentation that's explaining what I'm doing wrong. Can I add a suggested lat/lon as a parameter to ogr2ogr? Is it actually possible to do a conversion DXF to KML?

-

Ok, I managed to answer my own question. I use this procedure to convert DXF coordinates (in meters) to lon-lat, when I know what the center lon-lat will be. Let's call this point (center-lon, center-lat).

1. Use ogr2ogr to convert DXF to GeoJSON. This leaves all the big AutoCAD coordinates intact.
2. Make a script to read the GeoJSON file, parse it with json_decode (when using PHP).
3. Calculate the offset from (0,0) by making an average of the smallest and largest x and y coordinates.
4. With (center-lon, center-lat) as the center, convert GeoJSON coords (x,y) to (lon,lat):

lon = center-lon + (x - xoffset) * (360 / (e * cos(center-lat)))

lat = center-lat + (y - yoffset) * (360 / e)

With e the circumference of the earth in meters.

-
Keep in mind that ogr2ogr can reproject data too, with better accuracy than your formulas above. Specify the known spatial reference system with -s_srs, then use -t_srs EPSG:4326 to convert to lat/long. –  Mike T Jul 28 '12 at 7:09

It is impossible to create a kml file if your data is not georeferenced. KML uses decimal degrees as unit and not meters.

-
Thank you for your quick answer. Is there some way to transform a DXF? I forgot to mention that the data also has to be stored in a database. Sorry, this gives me a bit of a headache... –  Marvin80 May 16 '12 at 20:31
You need to know what system your data is in, than you can add it to the command line of ogr2ogr: -s_srs srs_def; anyway if you want to store in a database converting to kml is not needed. What database do you want to export to (update the question, don't add comments) –  johanvdw May 16 '12 at 20:34