Using SQLite in ArcGIS Python Scripts

Similar documents
Python: Beyond the Basics. Brittney White

Python: Beyond the Basics. Brittney White, Esri Jordan Douthitt, Pennsylvania State University

Using Python in ArcGIS Steven Beothy May 28, 2013

Traffic violations revisited

Databases. Course October 23, 2018 Carsten Witt

What s New for Developers in ArcGIS Maura Daffern October 16

LECTURE 21. Database Interfaces

SQLite. Nov 14, CS445 Pacific University 1 11/14/11

Using Python with ArcGIS

Using Python in ArcGIS Oli Helm May 2, 2013

Python Getting Started

PYTHON. Scripting for ArcGIS. writeoutput = Inputfc = ar. .ext.{) OUtpUt fc =.. Paul A. Zandbergen. axcpy random. .arcpy, Describe (' is.

Python for Oracle. Oracle Database Great for Data Store. Critical for Business Operations. Performance? Run in laptop? But how do you share it?

Using Python with ArcGIS

Introducing the ArcPy Data Access module. Dave Wynne Jason Pardy

CIS 192: Lecture 11 Databases (SQLite3)

CSE 115. Introduction to Computer Science I

8 Querying and Selecting Data

Alan Nichol Co-founder and CTO, Rasa

SQL I: Introduction. Relational Databases. Attribute. Tuple. Relation

Cisco s Jabber: A Closer Look with a Python Script

MySQL Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

Python: Beyond the Basics. Michael Rhoades

CSE 115. Introduction to Computer Science I

Understanding and Working with the OGC Geopackage. Keith Ryden Lance Shipman

LABORATORY OF DATA SCIENCE. Data Access: Relational Data Bases. Data Science and Business Informatics Degree

Introduction to pysqlite

NCSS: Databases and SQL

CS108 Lecture 19: The Python DBAPI

Landscape Gardening AQA GCSE Computer Science Controlled Assessment

15-388/688 - Practical Data Science: Relational Data. J. Zico Kolter Carnegie Mellon University Spring 2018

GISC 3200K. Python Programming for GIS. Final Project 12/8/2014. Chase Bennett

GNIS: West Virginia Summits Update Process, February 2012 Prepared by: Tyler R. Wylie, Kevin Kuhn

Workshop. Introducing SQL: A Foundation of Data Analytics. Robb Sombach University of Alberta Alberta School of Business

SI Networked Computing: Storage, Communication, and Processing, Winter 2009

Dynamic Dependent Drop-downs

Esri Geodatabase (File Geodatabase API) Reader/Writer

Python: Working with Feature Data. Ghislain Prince David Wynne

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Now go to bash and type the command ls to list files. The unix command unzip <filename> unzips a file.

Databases in Python. MySQL, SQLite. Accessing persistent storage (Relational databases) from Python code

Python Best Practices

Getting Started with SQL

Building Tools with Python. Evan Caldwell

INTRODUCTION TO DATA SCIENCE

MACBIO Marine and Coastal Biodiversity Management in Pacific Island Countries

Python and Databases

LABORATORY OF DATA SCIENCE. Data Access: Relational Data Bases. Data Science and Business Informatics Degree

Accessing and Administering your Enterprise Geodatabase through SQL and Python

Python Getting Started

ARCGIS ON ANDROID. 6 Jun Benjamin Lautenschlaeger

Bonus Content. Glossary

Key Terms. Attribute join Target table Join table Spatial join

Introduction to Column Stores with MemSQL. Seminar Database Systems Final presentation, 11. January 2016 by Christian Bisig

STATS Data analysis using Python. Lecture 3: Databases with SQL Some slides adapted from C. Budak

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

hereby recognizes that Timotej Verbovsek has successfully completed the web course 3D Analysis of Surfaces and Features Using ArcGIS 10

if row1.parcelid == row2.parcelid: print row.shape.area

Kaivos User Guide Getting a database account 2

STOP DROWNING IN DATA. START MAKING SENSE! An Introduction To SQLite Databases. (Data for this tutorial at

IEMS 5722 Mobile Network Programming and Distributed Server Architecture

CSE 530A ACID. Washington University Fall 2013

FEMA Floodplain Mapping

Essential Skills - RDBMS and SQL

GMA024F0. GridDB Web API Guide. Toshiba Digital Solutions Corporation 2017 All Rights Reserved.

Python: Getting Started. Ben

CF SQLite Code Generator User Manual V

Spatial Data Standards for Facilities, Infrastructure, and Environment (SDSFIE)

Potential analysis for rooftop farming in New Cairo. - A GIS-based model to calculate bed numbers and crop yield-

CSC326 Persistent Programming i. CSC326 Persistent Programming

Course Introduction & Foundational Concepts

CSC Web Programming. Introduction to SQL

ORACLE DATABASE 12C INTRODUCTION

ArcGIS Pro SDK for.net: An Overview of the Geodatabase API. Colin Zwicker Ling Zhang Nghiep Quang

Principles of Data Management

IBM DB2 UDB V7.1 Family Fundamentals.

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

python-tds Documentation

Python 3 Quick Reference Card

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Creating the Data Layer

Course Outline and Objectives: Database Programming with SQL

Transbase R PHP Module

phoenixdb Release Nov 14, 2017

DATABASE SYSTEMS. Database programming in a web environment. Database System Course, 2016

PostgreSQL and PL/Python. Daniel Swann Matt Small Ethan Holly Vaibhav Mohan

COE CST Seventh Annual Technical Meeting. Task #319. DebriSat Panel Preparation and Fragment Characterization for the Period: FY17 Q3

Why use an RDBMS? ❽ Data maintenance ❽ Standardized access ❽ Multi-user access ❽ Data protection

ESRI User Conference 2016 WITH

NCSS: Databases and SQL

WEB SECURITY: SQL INJECTION

DB tutorial using Database.NET dbforge Studio Express and SQLiteSpy

Bases de Dades: introduction to SQL (indexes and transactions)

Lecture 12 Programming for automation of common data management tasks

Programming and Database Fundamentals for Data Scientists

Presented By: John Sterrett. blog:

Flask Web Development Course Catalog

ITTIA DB SQL Em bedded Dat abase and VxWorks

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

CS378 -Mobile Computing. Persistence -SQLite

Transcription:

Using SQLite in ArcGIS Python Scripts Leslie H. Morgan, Ph.D. Leslie.Morgan@GISNuts.com

What is SQLite? The most widely deployed database engine worldwide. Transactional SQL database. Implements most of SQL92. Requires no configuration. Small. Fast. Reliable. Choose any three. http://www.sqlite.org/ Open source and in the public domain. It s Free!!!

SQLite Features Simple and easy to use Small Footprint Fast Supports terabyte-sized databases Self-contained and cross-platform ACID Transactions (Atomic, Consistent, Isolated, and Durable)

How do you access SQLite? The sqlite3 module is a C library that provides a SQL interface compliant with the DB-API 2.0 specification. Written by Gerhard Häring Part of the Python Standard Library (version 2.5+)

Where is the data stored? In a single *.db file In memory

Write standard ANSI SQL What can you do? Implements most of SQL92 (A few limitations) SELECT DISTINCT CLASS FROM GNIS_LA_2013 A Basic Example

Basic Example import sqlite3 conn = sqlite3.connect(r"c:\gisnuts\example.db") c = conn.cursor() c.execute('''select DISTINCT CLASS FROM GNIS_LA_2013''') lstunique = c.fetchall() conn.close()

SQL Examples Example 2: SELECT CLASS, Count(FEATURE_ID) FROM GNIS_LA_2013 GROUP BY CLASS ORDER BY Count(FEATURE_ID) DESC Example 3: SELECT FEATURE_CLASS, Count(FEATURE_ID) FROM GNIS_LA_2013 GROUP BY FEATURE_CLASS ORDER BY Count(FEATURE_ID) DESC LIMIT 10

Code import sqlite3 conn = sqlite3.connect(r"c:\gisnuts\example.db") c = conn.cursor() c.execute('''select CLASS, Count(FEATURE_ID) FROM GNIS_LA_2013 GROUP BY CLASS ORDER BY Count(FEATURE_ID) DESC''') lstuniquecount = c.fetchall() c.execute('''select CLASS, Count(FEATURE_ID) FROM GNIS_LA_2013 GROUP BY CLASS ORDER BY Count(FEATURE_ID) DESC LIMIT 10''') lstuniquecount10 = c.fetchall() conn.close()

SQL Example tbl1 tbl2 SELECT tbl1.id, tbl2.id FROM tbl1, tbl2 WHERE ((tbl1.address = tbl2.address) AND (tbl1.lastname = tbl2.lastname) AND (tbl1.firstname = tbl2.firstname) AND (tbl1.zipcode<>tbl2.zipcode))

Code import sqlite3 conn = sqlite3.connect(r"c:\gisnuts\example.db") c = conn.cursor() c.execute('''select tbl1.id1, tbl2.id2 FROM tbl1, tbl2 WHERE ((tbl1.address = tbl2.address) AND (tbl1.lastname = tbl2.lastname) AND (tbl1.firstname = tbl2.firstname) AND (tbl1.zipcode<>tbl2.zipcode))''') lstdiffzip = c.fetchall() conn.close()

How to Get Data from ArcGIS TableToNumPyArray (in_table, field_names, {where_clause}, {skip_nulls}, {null_value}) Notes: 1) Date, raster, and BLOB fields are not supported 2) Integer fields cannot contain nulls. rows can be skipped value can be assigned to null values 3) Pull only the fields you need and delete arrays when finished with them to avoid memory errors

How to Get Data from ArcGIS FeatureClassToNumPyArray (in_table, field_names, {where_clause}, spatial_reference, {explode_to_points}, {skip_nulls}, {null_value}) Notes: Geometry fields are also not supported. Geometry properties can be added to the array using tokens: SHAPE@XY The feature's centroid x,y coordinates SHAPE@TRUECENTROID The feature's true centroid x,y coordinates SHAPE@X The feature's x-coordinate SHAPE@Y The feature's y-coordinate SHAPE@Z The feature's z-coordinate SHAPE@M The feature's m-value SHAPE@AREA The feature's area SHAPE@LENGTH The feature's length

Example import arcpy, sqlite3 conn = sqlite3.connect(r":memory:") c = conn.cursor() tbl = r"c:\gisnuts\gisnuts.gdb\gnis_la_2013" array = arcpy.da.tabletonumpyarray(tbl, ["CLASS"]) c.execute("create table GNIS (Class text)") c.executemany("insert into GNIS values (?)", array) conn.commit() del array c.execute("select DISTINCT Class from GNIS") lstunique = c.fetchall() conn.close()

Example import arcpy, sqlite3 conn = sqlite3.connect(r":memory:") c = conn.cursor() fc = r"c:\gisnuts\gisnuts.gdb\gnis_la_2013" array = arcpy.da.featureclasstonumpyarray(fc, ["SHAPE@X", "SHAPE@Y"], '"PRIM_LAT_DEC" <> 0') c.execute("create table GNISCoords (X real, Y real)") c.executemany("insert into GNISCoords values (?,?)", array) conn.commit() del array c.execute("select Max(X), Min(X), Max(Y), Min(Y) from GNISCoords") bndbox = c.fetchall() conn.close()

What About Dates? NumPy arrays can not store dates SQLite does not have a Date field type Dates and times are stored as TEXT, REAL, or INTEGER values in SQLite: TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS") REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar. INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC

Date Example import arcpy, sqlite3 conn = sqlite3.connect(r":memory:") c = conn.cursor() fc = r"c:\gisnuts\gisnuts.gdb\gnis_la_2013" c.execute("create table GNISEdits (County text, FeatureID integer, DateCreated text, DateEdited text)") fields = ["COUNTY_NAME", "FEATURE_ID","DATE_CREATED", "DATE_EDITED"] whereclause = '"DATE_CREATED" > date \'08/28/2005\' or "DATE_EDITED" > date \'08/28/2005\'' with arcpy.da.searchcursor(fc, fields, whereclause) as cursor: for row in cursor: c.execute("insert into GNISEdits values (?,?,?,?)", row) conn.commit() c.execute('''select County, Count(FeatureID ), Max(DateCreated), Max(DateEdited) from GNISEdits GROUP BY County editcount = c.fetchall() conn.close() ORDER BY Count(FEATUREID) DESC;''')

Other Options There s always another way... There s always a better way... If I had more time, I would have written less code. (Thomas Edison + Mark Twain) SELECT CLASS, Count(FEATURE_ID) FROM GNIS_LA_2013 GROUP BY CLASS ORDER BY Count(FEATURE_ID) DESC Other Options for the Above Query: 1) ArcGIS Statistics Tool 2) Manipulate Numpy Array 3) Search Cursor and Loop with Dictionary

ArcGIS Statistics Tool import arcpy arcpy.env.overwriteoutput = 1 arcpy.env.workspace = r"c:\gisnuts\gisnuts.gdb" fc = "GNIS_LA_2013" outtbl = "ClassSummary" arcpy.statistics_analysis(fc,outtbl,"feature_id COUNT","CLASS") uniquelistcountarry = arcpy.da.tabletonumpyarray(outtbl, ["CLASS", "Frequency"]) uniquelistcountarry.sort(order = 'Frequency') uniquelistcount = uniquelistcountarry.tolist() uniquelistcount.reverse()

Manipulate Numpy Array import arcpy fc = r"c:\gisnuts\gisnuts.gdb\gnis_la_2013" GNISArray = arcpy.da.tabletonumpyarray(fc, ["CLASS", "Feature_ID"]) uniqueclasses = list(set(gnisarray['class'])) uniquelist = [] for value in uniqueclasses: uniquelist += [(value, len(gnisarray[gnisarray['class'] == value]))] del GNISArray uniquelist.sort(key=lambda x: x[1], reverse = True)

Search Cursor with Dictionary import arcpy fc = r"c:\gisnuts\gisnuts.gdb\gnis_la_2013" classdict = {} with arcpy.da.searchcursor(fc,"class") as cursor: for row in cursor: if classdict.has_key(row[0]): value = classdict[row[0]] + 1 classdict[row[0]] = value else: classdict[row[0]] = 1 uniquelist = classdict.items() uniquelist.sort(key=lambda x: x[1], reverse = True)

Thanks for Attending Questions? Leslie H. Morgan, Ph.D. Leslie.Morgan@GISNuts.com Twitter: @GISNuts LinkedIn: Leslie Morgan Presentation Link: www.gisnuts.com/scaug