Breaking a vector file up based on a unique attribute

It’s been awhile since I’ve done any Manifold stuff, so I thought I would show you how to break up a vector file based on a unique attribute.  A question was posted on the Manifold GIS forum  asking how to create new drawings (for those of you in an ESRI world – think shapefiles) based on the unique attributes in the database.

This is pretty easy to do, and I think might work really nicely within a Python paradigm (especially if you use psycopg2 that allows you to connect to Postgres).

 

Sub Main
  Set roads = document.ComponentSet("D")
  Set qry = document.NewQuery("Q",true)
  Set qry2 = document.NewQuery("Q2",true)
  qry.text = "select tp from D group by tp"
  for each rec in qry.Table.RecordSet
    thetype = rec.Data("tp")
    Set newdrw = Document.NewDrawing(thetype,roads.CoordinateSystem,true)
    qry2.text = "update D SET [Selection (I)] = true WHERE tp = " & chr(34) & thetype & chr(34)
    qry2.runEx true
    roads.copy(true)
    newdrw.paste
    roads.selectnone
  Next
End Sub

So what’s happening here?  Lines 1 and  17 just start and stop the subroutine.

Lines 2 – 4 set up three objects: a drawing called “D” that we set as a variable called roads; a query component called “Q”, and another query component called “Q2”.  In Manifold, a query component allows you to write SQL queries.

Line 5 – this is a basic SQL query where we are selecting a field called “tp” that has names of the road types (i.e. Primary, Secondary, etc..).  The GROUP BY statement basically just returns the unique road type names.  So, the result would be a table with the unique road types.

Line 6 – This is the start of a for loop.  when we say qry.Table.RecordSet, what we are actually doing is retrieving an object that is the result of the query we issued.  So, we are going to loop over that list of names.  The object we return is called rec and that represents the individual record in the list.

Line 7 – now that we are in the loop, the first thing we do is grab the first unique road type by getting the data from the rec object.  So, for that record, we grab the data value for the “tp” field.

Line 8 – This line creates a new drawing, using the name we got from the record in line 7, and we assign it the coordinate system for the original file we are working on.

Line 9 and 10 – This line creates a query that select all the records in the original drawing that have the unique name in the “tp” field, and then runs the query.  The result is an updated table where the selected records are highlighted.

Lines 11 – 13 – Here we do a little cut-and-paste gymnastics.  Line 11 copies the selected features in the original drawing (remember, these are just the features that have the “tp” name we are interested in for this part of the for loop.  Line 12 pastes those features into the new drawing we created in line 8, and line 13 unselects the lines.

Line 14 – When we get here, we’ve just created our first new drawing with the features that match the first road type.  The Next directive sends us back to the beginning of the for loop and we then process the next road type name.

That’s it.  It is really easy with Manifold scripting.  I’d post the Python solution, but I’m teaching GIS Programming this semester, and I think I’ll leave that to my students to do!

If you are interested in learning how to program with Manifold, Postgres/PostGIS, or build an enterprise GIS, check out my online courses here.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s