Shifting all features in vector dataset using Bash/OGR
I've designed Fiona (an OGR wrapper) to make this kind of processing simple.
from fiona import collection
import logging
log = logging.getLogger()
# A few functions to shift coords. They call eachother semi-recursively.
def shiftCoords_Point(coords, delta):
# delta is a (delta_x, delta_y [, delta_y]) tuple
return tuple(c + d for c, d in zip(coords, delta))
def shiftCoords_LineString(coords, delta):
return list(shiftCoords_Point(pt_coords, delta) for pt_coords in coords)
def shiftCoords_Polygon(coords, delta):
return list(
shiftCoords_LineString(ring_coords, delta) for ring_coords in coords)
# We'll use a map of these functions in the processing code below.
shifters = {
'Point': shiftCoords_Point,
'LineString': shiftCoords_LineString,
'Polygon': shiftCoords_Polygon }
# Example 2D shift, 1 unit eastward and northward
delta = (1.0, 1.0)
with collection("original.shp", "r") as source:
# Create a sink for processed features with the same format and
# coordinate reference system as the source.
with collection(
"shifted.shp",
"w",
driver=source.driver,
schema=source.schema,
crs=source.crs
) as sink:
for rec in source:
try:
g = rec['geometry']
g['coordinates'] = shifters[g['type']](
g['coordinates'], delta )
rec['geometry'] = g
sink.write(rec)
except Exception, e:
log.exception("Error processing record %s:", rec)
Update: I've put a different, tighter version of this script at http://sgillies.net/blog/1128/geoprocessing-for-hipsters-translating-features.
Using JEQL This can be done with three lines:
ShapefileReader t file: "shapefile.shp";
out = select * except (GEOMETRY), Geom.translate(GEOMETRY,100,100) from t;
ShapefileWriter out file: "ahapefile_shift.shp";
And though the post is tagged with python, since JEQL has already been mentioned, here's an example with JavaScript (using GeoScript).
/**
* Shift all coords in all features for all layers in some directory
*/
var Directory = require("geoscript/workspace").Directory;
var Layer = require("geoscript/layer").Layer;
// offset for all geometry coords
var dx = dy = 10;
var dir = Directory("./data");
dir.names.forEach(function(name) {
var orig = dir.get(name);
var shifted = Layer({
schema: orig.schema.clone({name: name + "-shifted"})
});
orig.features.forEach(function(feature) {
var clone = feature.clone();
clone.geometry = feature.geometry.transform({dx: dx, dy: dy});
shifted.add(clone);
});
dir.add(shifted);
});