paris.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. import json
  18. import pandas as pd
  19. from sqlalchemy import String, Text
  20. from superset import db
  21. from superset.utils import core as utils
  22. from .helpers import get_example_data, TBL
  23. def load_paris_iris_geojson(only_metadata=False, force=False):
  24. tbl_name = "paris_iris_mapping"
  25. database = utils.get_example_database()
  26. table_exists = database.has_table_by_name(tbl_name)
  27. if not only_metadata and (not table_exists or force):
  28. data = get_example_data("paris_iris.json.gz")
  29. df = pd.read_json(data)
  30. df["features"] = df.features.map(json.dumps)
  31. df.to_sql(
  32. tbl_name,
  33. database.get_sqla_engine(),
  34. if_exists="replace",
  35. chunksize=500,
  36. dtype={
  37. "color": String(255),
  38. "name": String(255),
  39. "features": Text,
  40. "type": Text,
  41. },
  42. index=False,
  43. )
  44. print("Creating table {} reference".format(tbl_name))
  45. tbl = db.session.query(TBL).filter_by(table_name=tbl_name).first()
  46. if not tbl:
  47. tbl = TBL(table_name=tbl_name)
  48. tbl.description = "Map of Paris"
  49. tbl.database = database
  50. db.session.merge(tbl)
  51. db.session.commit()
  52. tbl.fetch_metadata()