I have two layers with the same feature definitions named lyr and lyr_add. These layers were populated from a JSON based mapserver. I would like to append all of the features from lyr_add to lyr, but, when I use lyr.CreateFeature(feature) for a single feature, it is not appended. I would like to append the features to the lyr prior to writing it to a file. Running my python script does not result in any errors or exceptions. What should I modify in my code to successfully append the features?

An abbreviated version of my code that I have attempted is:

import ogr
import os
import sys
import urllib, json


xmin = 16501408.594
ymin = -4372525.989
xmax = 16520000
ymax = -4320000
extent = str(xmin) + ',' + str(ymin) + ',' + str(xmax) + ','+ str(ymax)

dict = {}

def count(url):
    """Returns a count of all of the features within the provided boundary"""
    url_count = '&where=1%3D1&returnCountOnly=true'
    url = url + url_count

    response = urllib.urlopen(url)
    data = json.loads(response.read())
    count = data[u'count']
    return count

def object_list(url):
    """Returns a list of the OBJECTID'S for all of the features within the provided boundary"""
    url_id = '&returnIdsOnly=true'
    url = url + url_id
    response = urllib.urlopen(url)
    data = json.loads(response.read())
    list = data['objectIds']
    return list

url_server = "http://maps.six.nsw.gov.au/arcgis/rest/services/sixmaps/LPIMap/MapServer/"
url_2 = "/query?geometry="
url_3 = "&geometryType=esriGeometryEnvelope"
url_4 = "&f=pjson"

for key in dict:
    url = url_server + str(dict[key]) + url_2 + extent + url_3 + url_4
    count = count(url)
    if count == 0:
        ds = ogr.Open(url)
        lyr = ds.GetLayer()
        if count > 1000:
            object_list = object_list(url)
            low_bound = object_list[x*1000]
            if ((x+1)*1000) > count:
                up_bound = object_list[-1]
                up_bound = object_list[((x+1)*1000-1)]

            url_loop = "&where=OBJECTID+>%3D+" + str(low_bound) + "+AND+OBJECTID+<%3D+" +str(up_bound)
            url_2 = url + url_loop
            ds_add = ogr.Open(url_2)
            lyr_add = ds_add.GetLayer()
            for i in range(0, lyr_add.GetFeatureCount()):
                feature = lyr_add.GetFeature(i)
            del ds_add

        del ds, lyr    

1 Answer 1


You need to have "write" access to the data source. In this case the data source is a web page resulting from a query. One solution is to create a copy of the data source in the computer's memory, and, then add to that memory:

        #create an output datasource in memory
        source = mem_driver.CreateDataSource('memData')
        mem = source.CopyLayer(lyr, key)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.