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


Python core.QgsJSONUtils类代码示例

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


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

示例1: testStringToFeatureList

    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,代码行数:47,代码来源:test_qgsjsonutils.py

示例2: testEncodeValue

 def testEncodeValue(self):
     """ test encoding various values for use in GeoJSON strings """
     self.assertEqual(QgsJSONUtils.encodeValue(NULL), "null")
     self.assertEqual(QgsJSONUtils.encodeValue(5), "5")
     self.assertEqual(QgsJSONUtils.encodeValue(5.9), "5.9")
     self.assertEqual(QgsJSONUtils.encodeValue(5999999999), "5999999999")
     self.assertEqual(QgsJSONUtils.encodeValue("string"), '"string"')
     self.assertEqual(QgsJSONUtils.encodeValue("str\ning"), '"str\\ning"')
     self.assertEqual(QgsJSONUtils.encodeValue("str\ring"), '"str\\ring"')
     self.assertEqual(QgsJSONUtils.encodeValue("str\bing"), '"str\\bing"')
     self.assertEqual(QgsJSONUtils.encodeValue("str\ting"), '"str\\ting"')
     self.assertEqual(QgsJSONUtils.encodeValue("str\\ing"), '"str\\\\ing"')
     self.assertEqual(QgsJSONUtils.encodeValue("str\\ning"), '"str\\\\ning"')
     self.assertEqual(QgsJSONUtils.encodeValue("str\n\\\\ing"), '"str\\n\\\\\\\\ing"')
     self.assertEqual(QgsJSONUtils.encodeValue("str/ing"), '"str\\/ing"')
     self.assertEqual(QgsJSONUtils.encodeValue([5, 6]), "[5,6]")
     self.assertEqual(QgsJSONUtils.encodeValue(["a", "b", "c"]), '["a","b","c"]')
     self.assertEqual(QgsJSONUtils.encodeValue(["a", 3, "c"]), '["a",3,"c"]')
     self.assertEqual(QgsJSONUtils.encodeValue(["a", "c\nd"]), '["a","c\\nd"]')
     self.assertEqual(QgsJSONUtils.encodeValue({"key": "value", "key2": 5}), '{"key":"value",\n"key2":5}')
     self.assertEqual(
         QgsJSONUtils.encodeValue({"key": [1, 2, 3], "key2": {"nested": "nested\\result"}}),
         '{"key":[1,2,3],\n"key2":{"nested":"nested\\\\result"}}',
     )
开发者ID:mbernasocchi,项目名称:QGIS,代码行数:24,代码来源:test_qgsjsonutils.py

示例3: print

from qgis.utils import iface

geojson_contributors = os.path.join(
    os.path.dirname(QgsApplication.developersMapFilePath()),
    'contributors.json'
)
geojson_contributors_string = codecs.open(
    geojson_contributors,
    encoding='utf-8'
).read()

layer = iface.activeLayer()

# Encodes a value to a JSON string representation, adding appropriate
# quotations and escaping where required.
print(QgsJSONUtils.encodeValue([{"name": "George", "age": 34, "size": 1.69}]))

fields = QgsFields()
fields_list = [
    QgsField("name", QVariant.String),
    QgsField("age", QVariant.Int),
    QgsField("size", QVariant.Double)
]

for f in fields_list:
    fields.append(f)

feature = QgsFeature(fields)
feature.setGeometry(QgsGeometry.fromPoint(QgsPoint(60, 5)))
feature.setAttributes(["George", 34, 1.69])
开发者ID:GEO-IASS,项目名称:pyqgis-samples,代码行数:30,代码来源:qgis-sample-QgsJSONUtils.py


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