• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
North River Geographic Systems Inc

North River Geographic Systems Inc

Geospatial Problem Solving

  • Home
  • About NRGS
  • Training
  • Blog
  • Resources
    • QGIS Tutorials and Hacks
    • GDAL Tutorials and Hacks
    • PostGIS Tutorials and Hacks
    • GRASS Tutorials and Hacks
    • OSM Tutorials and Hacks
  • Services
    • Support and Services
    • Tennessee NG911 Address Server
    • Forestry Database Services
    • Conservation GIS
  • Portfolio
  • Show Search
Hide Search

Open Source GIS

Adding Triggers to Geopackage

In between work I get a chance to play around with software. I’ve been digging around postgis more and more these days trying to get a better understanding of what it will do or won’t do. It’s a never ending learning experience for me. Sometimes it pays off – the never ending tutorial left me suggesting we dump a database to a geopackage.

I’m great at using geopackage in sentences like “USE GEOPACKAGE” and “Hey – what do you think about geopackage?”. Since I’m actually officially using it I decided to do some digging. It’s a SQLite container. Since it’s a SQLite container I should be able to do some things with it – like…..

spatialite gui

I can use the spatialite gui and throw SQL against it and change the tables.

While talking to another person the question started with ‘Well – why can’t we just do the data collection in geopackage?’.  Which we did some back and forth and in some cases this would be acceptable vs doing a full postgis install.

So my first question is: Can I make a trigger in a geopackage? The answer is yes. For those of you who don’t know what a trigger does – it responds to some action in a database. In postgresql, I put multiple triggers in a database for a client. Some triggers calculate lengths and some areas and some make a note of who edited a feature.

After some digging I found the syntax and wrote a trigger for SQLite/Geopackage in the form of:

CREATE TRIGGER update_esn AFTER INSERT
ON addresses
BEGIN
update addresspoints set “esn” = (select esn from polygon where st_within(new.geom, geom));
END;

I used the spatialte gui to add the trigger. So if I pass this off to a client they can edit an address and have it automatically pull in the emergency service number. Magic? Eh – close enough for me.

QGIS Screenshot

While it’s not going to work for a multi-user situation (or will it – I’ve not investigated) – it works. So now I’m adding widgets to QGIS to aid in data collection.

Knoxville FOSS4G 2018 – August 13th 2018

It’s on.

Website: https://wiki.osgeo.org/wiki/20180813-knoxville-foss4g

Date: August 13th 2018

Location:
Plant Biotech Building
UT Ag Campus
2505 EJ Chapman Dr
Knoxville, TN 37996

I guess it might be safe to call this the Second Annual FOSS4G meeting in TN. We’re headed back to Knoxville on August 13th 2018 for another day of Free and Open Source excitement for GIS. Last year we had about 50 people turn up to hear talks ranging from OSM to LIDAR to Image Classification.

We’re also looking for Sponsors. Right now we have 3 and some money has already been promised. Most likely this even will cost $10 this year to offload some of the headache I got to experience last year as “Money Juggler”.  Depending on sponsors this $10 may drop. Registration will be open soon – that’s on my list for next week once I get a few more things settled.

Good people – if you are in the area turn up and come to this. It’s a good time. Good Discussion. Come see how you can benefit from FOSS4G in your world.

Last Year’s Hurricane Season in the USVI

So I’ll talk about this finally. I usually don’t talk about “work” unless it’s in generalities about software and processes. I probably should talk about work more.

I’ve never been in a hurricane. I think so far I’ve been in about a handful of earthquakes, stood on one volcano, and I think I’ve seen one tornado. Not that I’m looking to be in a natural disaster – I just don’t have a frame of reference for what happens during a hurricane or a tornado.

If you flash back to early September 2017 the Caribbean was gearing up for a hurricane and. Turns out two hit close together. I had the chance to work in the US Virgin Islands on an addressing project with AppGeo and Spatial Focus several years ago. It’s been a “wait and see” type of deal. We did a little and watch as red tape and things slow it down. I called a few friends down there before the hurricanes hit and wished them well. Overall – there wasn’t a ton of fear but a lot of concern. I was concerned because the 911 system isn’t great as the addressing isn’t wonderful in the USVI.

My memory is getting fuzzy – but it seems like both hurricanes hit and it was a while before anyone could call/email me. Maybe it was late September or October before the phone rang. They needed help to figure out where the tarps needed to go.  FEMA/USACE was headed down to bring tarps and supplies – maybe they were already there. A sneaker net of GIS data had cranked itself up on the island as thumb drives were passed around. I started trying to figure out “what made sense with the base data I had available”. I started digging into what USACE and What FEMA would do if your roof was damaged. More or less if you had 50% of the structure of the roof left they could put a tarp on it. I came up with a plan and contacted AppGeo and they tossed a person my way.

Structures were categorized:

  • 1 – 25% Roof Damage
  • 26 to 50% Roof Damage
  • 51 to 75% Roof Damage
  • 76 to 100% Roof Damage
  • Structure Destroyed
  • No Damage
  • No Imagery

NOAA flew imagery of the area. The imagery was a bit rough around the edges but it worked. Actually – let me back up – for what it was it was awesome. Granted it didn’t lend itself to any sort of automatic classification as there was a lot of building lean and some missing imagery. It worked though. That was a great free resource by NOAA.

Hey that’s a lot of points. Yes – 48380 points covering three islands. Granted I didn’t get every house as I had nothing much to start out with except OSM and some USDA NAIP imagery. I would guess I’m within 10% of an accurate building count. Some people have offices/live in shipping containers down there. WHICH – when I was there made me go “awesome” but when looking at the imagery at a shipping container blowing a half mile across a field – not so awesome.

It did give me flash backs to my old remote sensing days with the government. Although this was less refined – it was a game in the afternoons to see how many points I could collect in an hour. Some neighborhoods were easy. Some not so much as there wasn’t much left. The other nice thing that happened was sometime around December I switched over to QGIS 3.0 and started seeing what I could/couldn’t do with this almost released software. Several bug reports were filed so more good came out of this.

The data started making it’s way out to one agency after another. My big fear then was “what if someone uses this beyond ‘roofs'”. I wasn’t doing a full blow damage assessment because I was seeing some evidence that maybe the roof survived but the bottom floors didn’t. Plus I wasn’t on location.

It took me a bit longer to finish than I wanted – Volunteer projects tend to be the last thing I work on most days.

I did start pulling numbers out of some of the work – this is for St John Island:

Count Classification Percent
1487 1 – 25% Roof Damage 41.42
326 26 to 50% Roof Damage 9.08
131 51 to 75% Roof Damage 3.65
126 76 to 100% Roof Damage 3.51
1007 No Damage 28.05
17 No Imagery 0.47
496 Structure Destroyed 13.82
3590 Total 100

So 25% of the houses had no roof damage. 60% had something happen. More or less 15% were leveled. I’ve heard ancillary stories where almost all houses had water coming in them and in a warm tropical climate you get mold. Mold would make the house unusable for me.

Anyway – I worry about the 2018 Hurricane season that is upon us. Maybe it will be quiet – maybe it won’t be. AppGeo has Mapgeo for the islands. It worked out well. I’m going to try to “build” a database here so in case something happens and I’m not starting from scratch in case I get to help out again. I hope I don’t help as nothing happens.

 

I didn’t know it was called Refactor Fields in QGIS

The title may be the worst part of this post. Hopefully. Anyway – here’s what happened. I will explain it in all of it’s goofy detail with some hope that by the end you’ll do the same thing I did.

I’ve a client that is currently  making me push what I can do with QGIS/PostGIS. We hit a technical bump with data collection and to fix it they walked in with a new app they had purchased from some online app store that records data from their super accurate GPS Unit. The manufacturer of the GPS unit had one – but they found this scratched the itch more than the one they could have used….and the one they were using. My eye didn’t twitch much. I just cautioned against randomly doing things with your setup. I mean what if it makes a file you can’t import into your database. What if………

They have in postgresql a table that was more or less this: column1, column2, column3 . This app could create several data formats that ultimately me with: Foo1, column2, Bar3. You can’t just “copy and paste” the data in because the columns need to match. So I need to force it to match. No matter what trick I tried it wasn’t easy BUT some of them did work.

An Example of my thought process.  My python skills aren’t great – BUT – using the psycopg2 library, some ogr, and “the google” I was able to connect and change my data and push into Postgresql. More or less you parse out the columns and match them to the PostgreSQL table:

import psycopg2
import osgeo.ogr 
#import shapely

shapefile = osgeo.ogr.Open("/home/rjhale/gis/shapefiles/monument.shp")
layer = shapefile.GetLayer(0)

conn = psycopg2.connect("dbname='database' user='me' \ 
       host='localhost' password='itsasecret'") 
cur = conn.cursor()

for i in range(layer.GetFeatureCount()):
      feature = layer.GetFeature(i)
      descriptio = feature.GetField("descriptio")
      pos_x_sf = feature.GetField("Pos_x_sf")
      pos_y_sf = feature.GetField("Pos_y_sf")
      elev_z_sf = feature.GetField("Elev_z_sf")
      photos = feature.GetField("photos")
      local_date = feature.GetField("local_date")
      pdop = feature.GetField("pdop") 
      #Get feature geometry
      geom = feature.GetGeometryRef()
      #Convert geometry to WKT format
      wkt = geom.ExportToWkt()
      #Insert data into database, converting WKT geometry to PostGIS geometry
      cur.execute("INSERT INTO test.monument(descriptio, \ 
                   pos_x_sf, pos_y_sf, elev_z_sf, \
                   photos, local_date, pdop, geom) \
                   VALUES ('{}', '{}', '{}', '{}', '{}', '{}', '{}', \ 
                   ST_GeomfromText('{}',6447))"\
                  .format(descriptio, pos_x_sf, pos_y_sf, elev_z_sf, photos, \
                  local_date, pdop, wkt))

 

Holy crap. It worked. I really need to up my python game BUT – no one wanted to run something from cmd line. MAKE A PLUGIN! Which I considered. In my digging I found the refactor fields tool  in QGIS. It’s very similar to loading data into a database with ArcGIS for those of you stuck on that side of the fence.

Refactor Fields

So how does it work? It’s easy. Load your data that doesn’t quite match anything as the input layer. Load the table you want it to match in the “load fields from layer” part at the bottom. Click Load Fields.

loaded refactor fields

So what just happened? Refactor fields took my shapefile and matched it to the columns in PostgreSQL. If it can’t “guess” you have the option to make it match by typing in the fields. It generates a new layer that I can “copy and paste” into the database or import into the database. For some of the data I’m having to clean out the fields – so things that don’t match in the shapfile I’m removing from the new layer using this processing tool. Which – maybe I need to add those fields to PostgreSQL. I really want this table stabilized before we start. Although I’ve not tried it I’m guessing this works with any layer you want – I’m just using it against postgresql. I also will say this probably won’t match projections – so get your projection worries figured out before hand – this just handles the attributes.

Right now I’m  looking at a model so they can ultimately just “ClICK SOMETHING”. We all like menus. Mostly.

Anyway – load your data. QGIS has many tricks to make your life easy and I had no clue. I did learn something on the Python side of life though and for that I’m happy even though it turned into a dead-end.

 

 

 

Docker for the Geo People

This might get lengthy. You have been warned. Docker people – settle down I’m new at this.

I dabbled in Docker  to get pgadmin4 up and running. A few months ago I tweeted (and I couldn’t find it) something derogatory about docker and virtual machines and what not. I was poked at for tweeting it. A few weeks after one person lamented why do the old school guys not embrace docker….because I’m old and that’s the way I likes it.

So something like 5 years ago I discovered Virtual machines. I was primarily running ESRI on everything and didn’t run anything in a Virtual Machine. Everything “just ran” and there was no need for one more complication in my life. One day I got a call and found myself going out of the country for a bit and I had been experimenting with dual booting my computer between windows/linux. Some more reading and I had loaded linux on my main workstation and moved my windows environment to a VM. I set up a VPN and suddenly everything is at my fingertips through SSH or VNC.

A couple of years after that – vagrant popped up on my radar. I could quickly deploy a ubuntu/debian server with fun things like a web server or a database or whatever I wanted. I could play with it and then delete it and adjust my VagrantFile to load more or less or compile software at startup.

Docker pops up. I’ve had a few false starts with it. When I started this I was determined I was going to write my own dockerfile. So I did. It didn’t work. I became irritated. I ended up getting one running. I threw a command against it. It left a container. So then I had images….and containers….and I did something else and I had two containers and I felt like I was junking up my computer. At one point I had like 15 containers and to me a VM seemed more natural. It’s a certain size. I know what is going to happen.  After RTFM’ing– I realized I didn’t have to “leave containers” all over the place. I didn’t have to keep an image. I could start and stop docker when I wanted.

So to really have some fun – I loaded the Kartoza DockerFile for PostGIS/Postgresql  (read the directions). In short I did this (and I’m on linux – but you can do this on windows): 

  1. docker pull kartoza/postgis
  2. sudo docker run –name “postgis” -p 25432:5432 -d -t kartoza/postgis
  3. psql -h localhost -U docker -p 25432 -d gis

I attached to it through the PSQL prompt and QGIS. I loaded data. It was fine. It was normal. If you think of it as a separate service running on your computer things get more “normal”. I have a lot of services I deal with every day from postgres to  ssh to dropbox etc. It’s just separate from the computer so you can’t break things. It’s self contained.

Maybe instead of a normal postgresql install I just load docker on my laptop and crank up Postgis/Postgresql when I need it. Maybe maybe maybe. I’m probably going to put pgadmin4 (through docker) on the postgis “server” I have running over in the corner. Maybe I get QGIS Server up and running in docker?

So I said it simplifies everything In my head……….once I “learn” enough to be proficient with it. Here’s my old man yelling at cloud moment.

ONE MORE THING I HAVE TO LEARN. Which may be why my old school comrades aren’t so quick to embrace this. I’m still flailing with GIT  on a good day.  So 18 years ago I had my first ArcGIS Desktop Experience. 18 YEARS AGO. I love this side of FOSS4G though – I have to learn something new. Which is why I sometimes hate the FOSS4G side of life – I can’t get comfortable and just go “Hey look I’m clicking buttons”. Some days there is comfort in clicking buttons – but I do the clients a disservice if all I can do is click buttons.

So I’m not going to a docker conference. I’m not going to write software and push it into docker for the masses. This isn’t a hammer for every nail I see……..BUT it is something you need to be paying attention to over the long haul. It’s another tool in your gistoolbox to make your life a bit more fun.

« Previous Page
Next Page »

Primary Sidebar

Calendar

July 2022
M T W T F S S
 123
45678910
11121314151617
18192021222324
25262728293031
« Jun    

What we talk about

addresses ArcGIS ASPRS Athens Atlanta Beer Cartography Chattanooga Community Conference Data Do Some Good Education ESRI Forestry FOSS4G foss4gna Fulcrum gaurisa ga urisa Gdal Georgia GeoServer GIS GISP Google GRASS Lidar MAGS open source OpenStreetMap osgeo OSM postgis Python QGIS raleigh Rambling Small Business Tennessee TNGIC Topology Training URISA usvi

North River Geographic Systems Inc

  • Home
  • About NRGS
  • Training
  • Blog
  • Resources
  • Services
  • Portfolio