当前位置: 首页>>代码示例>>Python>>正文


Python QgsJSONUtils.stringToFeatureList方法代码示例

本文整理汇总了Python中qgis.core.QgsJSONUtils.stringToFeatureList方法的典型用法代码示例。如果您正苦于以下问题:Python QgsJSONUtils.stringToFeatureList方法的具体用法?Python QgsJSONUtils.stringToFeatureList怎么用?Python QgsJSONUtils.stringToFeatureList使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在qgis.core.QgsJSONUtils的用法示例。


在下文中一共展示了QgsJSONUtils.stringToFeatureList方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testStringToFeatureList

# 需要导入模块: from qgis.core import QgsJSONUtils [as 别名]
# 或者: from qgis.core.QgsJSONUtils import stringToFeatureList [as 别名]
    def testStringToFeatureList(self):
        """Test converting json string to features"""

        fields = QgsFields()
        fields.append(QgsField("name", QVariant.String))

        # empty string
        features = QgsJSONUtils.stringToFeatureList("", fields, codec)
        self.assertEqual(features, [])

        # bad string
        features = QgsJSONUtils.stringToFeatureList("asdasdas", fields, codec)
        self.assertEqual(features, [])

        # geojson string with 1 feature
        features = QgsJSONUtils.stringToFeatureList(
            '{\n"type": "Feature","geometry": {"type": "Point","coordinates": [125, 10]},"properties": {"name": "Dinagat Islands"}}',
            fields,
            codec,
        )
        self.assertEqual(len(features), 1)
        self.assertFalse(features[0].geometry().isEmpty())
        self.assertEqual(features[0].geometry().wkbType(), QgsWkbTypes.Point)
        point = features[0].geometry().geometry()
        self.assertEqual(point.x(), 125.0)
        self.assertEqual(point.y(), 10.0)
        self.assertEqual(features[0]["name"], "Dinagat Islands")

        # geojson string with 2 features
        features = QgsJSONUtils.stringToFeatureList(
            '{ "type": "FeatureCollection","features":[{\n"type": "Feature","geometry": {"type": "Point","coordinates": [125, 10]},"properties": {"name": "Dinagat Islands"}}, {\n"type": "Feature","geometry": {"type": "Point","coordinates": [110, 20]},"properties": {"name": "Henry Gale Island"}}]}',
            fields,
            codec,
        )
        self.assertEqual(len(features), 2)
        self.assertFalse(features[0].geometry().isEmpty())
        self.assertEqual(features[0].geometry().wkbType(), QgsWkbTypes.Point)
        point = features[0].geometry().geometry()
        self.assertEqual(point.x(), 125.0)
        self.assertEqual(point.y(), 10.0)
        self.assertEqual(features[0]["name"], "Dinagat Islands")
        self.assertFalse(features[1].geometry().isEmpty())
        self.assertEqual(features[1].geometry().wkbType(), QgsWkbTypes.Point)
        point = features[1].geometry().geometry()
        self.assertEqual(point.x(), 110.0)
        self.assertEqual(point.y(), 20.0)
        self.assertEqual(features[1]["name"], "Henry Gale Island")
开发者ID:mbernasocchi,项目名称:QGIS,代码行数:49,代码来源:test_qgsjsonutils.py

示例2: QgsFeature

# 需要导入模块: from qgis.core import QgsJSONUtils [as 别名]
# 或者: from qgis.core.QgsJSONUtils import stringToFeatureList [as 别名]
for f in fields_list:
    fields.append(f)

feature = QgsFeature(fields)
feature.setGeometry(QgsGeometry.fromPoint(QgsPoint(60, 5)))
feature.setAttributes(["George", 34, 1.69])

# Exports all attributes from a QgsFeature as a JSON map type.
with codecs.open('feature.json', 'wb', encoding='utf-8') as f:
    json_text = QgsJSONUtils.exportAttributes(feature)
    f.write(json_text)

# Attempts to retrieve the fields from a GeoJSON string representing a
# collection of features.
fields = QgsJSONUtils.stringToFields(
    geojson_contributors_string,
    QTextCodec.codecForName('UTF-8')
)

for f in fields:
    print(f.name(), f.type())

# Attempts to parse a GeoJSON string to a collection of features.
features = QgsJSONUtils.stringToFeatureList(
    geojson_contributors_string,
    fields,
    QTextCodec.codecForName('UTF-8')
)
print(features)
开发者ID:GEO-IASS,项目名称:pyqgis-samples,代码行数:31,代码来源:qgis-sample-QgsJSONUtils.py


注:本文中的qgis.core.QgsJSONUtils.stringToFeatureList方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。