LayerUtil.py 866 Bytes
# coding=utf-8
#author:        4N
#createtime:    2021/8/12
#email:         nheweijun@sina.com
from osgeo.ogr import Layer,Feature
import json


class LayerUtil:

    @classmethod
    def layer_to_feature_collection(cls,layer,limit=None):
        '''
        图层转feature collection
        :param layer:
        :param limit: 读取限制数
        :return:
        '''
        feature_collection = dict()
        feature_collection["type"]="FeatureCollection"
        feature_collection["features"] = []
        count = 0
        for feature in layer :
            feature:Feature = feature
            feature_collection["features"].append(json.loads(feature.ExportToJson()))
            #超过限制不再读取
            if limit:
                if count>limit:
                    break
        return feature_collection