EDIT (PARTLY - SOLVED): Python code that is following solves part of the problem. The subdivision is made but the result is written for only one of the sub polygons to the new shape file. Any additional help to finish this algorithm in Python would be greatly appreciated.
import os
import ogr
import sys
import numpy as np
#Driver
driver = ogr.GetDriverByName('ESRI Shapefile')
#Working Directory Path
ws = 'C:/data/'
#Make ws the working directory
os.chdir(ws)
#vector path
in_data = 'data.shp'
#user inputs; how many splits per axis
xSplit = 2
ySplit = 2
#This is a three dimensional array (In numpy Z,Y,X values)
dCoords = np.ones((2,ySplit+1,xSplit+1))
#Input polygons
source_ds = ogr.Open(in_data,0)
source_layer = source_ds.GetLayer(0)
source_features = source_layer.GetFeatureCount()
#Output polygons
target_ds = driver.CreateDataSource('split.shp')
if target_ds is None:
print 'Creation of output file failed.\n'
sys.exit(1)
geom_type = ogr.wkbPolygon
target_layer = target_ds.CreateLayer('split',None,geom_type)
if target_layer is None:
print 'Creating of layer failed.\n'
sys.exit(1)
field_defn = ogr.FieldDefn('parcel_no',ogr.OFTInteger)
field_defn.SetWidth(30)
if target_layer.CreateField(field_defn) !=0:
print 'Creating plot_num field failed.\n'
sys.exit(1)
featureDefn = target_layer.GetLayerDefn()
#Get the polygons from the layer
for i in xrange(0,source_features):
source_poly = source_layer.GetFeature(i)
source_plot = source_poly.GetField('id')
source_geom = source_poly.GetGeometryRef()
#Get the corner coords of the quad
cx = []
cy = []
pts = source_geom.GetGeometryRef(0)
points = []
for p in xrange(pts.GetPointCount()):
cx.append(pts.GetX(p))
cy.append(pts.GetY(p))
dx0 = (cx[1] - cx[0]) / xSplit
dx1 = (cx[1] - cx[2]) / ySplit
dx2 = (cx[2] - cx[3]) / xSplit
dx3 = (cx[0] - cx[3]) / ySplit
dy0 = (cy[1] - cy[0]) / xSplit
dy1 = (cy[1] - cy[2]) / ySplit
dy2 = (cy[2] - cy[3]) / xSplit
dy3 = (cy[0] - cy[3]) / ySplit
for ys in xrange(0,ySplit):
x1 = cx[3] + dx3 * ys
y1 = cy[3] + dy3 * ys
x2 = cx[2] + dx1 * ys
y2 = cy[2] + dy1 * ys
dx2 = ( x2 - x1 ) / xSplit
dy2 = ( y2 - y1 ) / xSplit
for xs in xrange(0,xSplit):
x3 = x1 + dx2 * xs
y3 = y1 + dy2 * xs
#Calculate Grid Coordinates
dCoords[0][xs][ys] = x3
dCoords[1][xs][ys] = y3
#Build Grid
for i in xrange(0,ySplit - 1):
for j in xrange(0,xSplit - 1):
#Create an empty ring geometry
ring = ogr.Geometry(ogr.wkbLinearRing)
ring.AddPoint(dCoords[0][j][i],dCoords[1][j][i])
ring.AddPoint(dCoords[0][j][i+1],dCoords[1][j][i+1])
ring.AddPoint(dCoords[0][j + 1][i + 1],dCoords[1][j + 1][i + 1])
ring.AddPoint(dCoords[0][j + 1][i],dCoords[1][j + 1][i])
ring.AddPoint(dCoords[0][j][i], dCoords[1][j][i])
target_poly = ogr.Geometry(ogr.wkbPolygon)
target_poly.AddGeometry(ring)
target_feature = ogr.Feature(featureDefn)
target_feature.SetGeometry(target_poly)
target_feature.SetField('id',source_plot)
target_layer.CreateFeature(target_feature)