# Could we have a 'Code library'?

I was wondering if it would be possible to collaborate on a code library for GIS functions on this site.

So many times I am rewriting functions I know I have either used before, or know someone must have solved that 'riddle' before.

Would it be possible to have an area solely for reusable code from all of us?

• Something like the ActiveState Python Cookbook would be pretty slick. – Chad Cooper May 18 '11 at 10:51
• I've put in a feature request here to make it easier to find code in gis.SE. – Kirk Kuykendall Dec 21 '11 at 18:07
• It is a very strong idea. – user17614 Aug 4 '13 at 14:41

One solution, working within SE technology, is to create tags signaling that a thread includes effective code. When an upvoted or accepted reply contains code, we can (retroactively if necessary) apply the appropriate "code" tag. This would make the solution accessible through searches. This would be more efficient and effective than trying to create a separate place for code only. Moreover, the search would disclose the code within the context of a question, other answers, and the comment streams.

All you really need to think through is whether a single generic code tag would work or whether you want language- or platform-specific tags like code-sql, code-python, etc. I would recommend the first because you can refine your searches with keywords, like "sql" or "python", to ferret out the language in which you're interested (if that matters so much).

Note that a code tag exists but does not yet have a well-defined meaning: apart from this thread, only two others have been tagged with it. So it's there for us to use. Mods can support such use by means of a FAQ entry and a wiki entry for the tag, but its maintenance (i.e., applying the tag appropriately) would have to be done by everyone.

• That would be a move forward. – user681 May 18 '11 at 17:34
• This would be a great usage for these 'code-xxx' tags. It would seem that, other than cleaning up posts and their tags, the 'tag wiki's' would have to be edited/updated. Since I am still about 1900 reputation away from doing this, how long would it take for a wiki I would write to be peer reviewed? – Jeremy J May 23 '11 at 2:16
• @Jeremy An edit attempt is automatically flagged for review. This brings it to the attention of all mods. Typically, the review will happen within minutes to hours and almost certainly within a day. – whuber May 23 '11 at 15:07
• Since tags are applied to questions - and not answers - it is a might be a bit awkward for the person providing code in an answer to add a code tag to the question. Maybe the questioner wasn't interested in getting code. I ran into this issue with Esri's forums and ended up using kkeywords in the body of the answer to make it easier to search for it later. – Kirk Kuykendall Dec 16 '11 at 4:04
• Also, I don't see a way in the Advanced Super Ninja Search Options to search for posts that have code sample markup in the body. Maybe we should request that SE provide this as a search option. – Kirk Kuykendall Dec 16 '11 at 4:13

We already have a place for this -- you're looking at it! ;-) It's okay to ask and answer your own question. If you have a working algorithm or snippet that has solved a significant problem you faced and you want to place somewhere for later retrieval and sharing with others, this site or Stack Overflow is a good place (depending on the nature of the code and problem). Simply ask a question outlining the challenge faced, and then answer with your solution.

Here are two examples of contributions to "the GIS code library" from my own history here, Accessing ArcObjects from Python? and Arcmap: attach python script to a button?. The latter is especially interesting to me, because although I had a solution ready to post a much better one was contributed.

I do advise paying attention to the community dynamics at play as talked about in Somebody is answering too many of their own questions and How much code is appropriate for an answer? among other meta topics.

Instead of trying to bend GIS Stack Exchange to store code libraries, GitHub is designed as:

Powerful collaboration, code review, and code management for open source and private projects

In terms of ArcGIS alone there are already over 1,000 repositories there.

On the other hand you will find a huge number of code snippets here at GIS Stack Exchange, tagged by languages such as C#, which may often save you needing to look elsewhere.

• The problem with GitHub is that most of the code is embedded in larger projects and it can be next to impossible to search for specific functionality or identify the context/purpose of poorly-commented code, which is where the Q&A format of SE would shine. GitHub is more useful if you already know which repository (or at least which user) you're looking for. – nmpeterson Sep 15 '14 at 19:16

It would be really great to have such a code library, but I see a few issues with this:

• Every piece of code has to be generic in nature, so it could be used by every software
• The code would, hence, be more of a pseudo-code than actual code
• How would we overcome the differences in functionalities across various software?
• Programming language dependency (it could however be resolved if we stick to point 2 above

Just few thoughts...

• I am sue there are generic problems, point in polygon, find point on line, etc, that could be put up. I would have thought the logic shown in one version of code, say in VB, could be ported over to any language, syntactical problems should be fairly easy to overcome? – Hairy May 18 '11 at 9:19
• I agree. It would be nice to have a searchable content base which can be referred from anywhere. How do you propose to start it on StackExchange...a new thread, may be? – ujjwalesri May 18 '11 at 9:30
• I was hoping a moderator could come on board and set up an area? – Hairy May 18 '11 at 9:42
• @Hairy The famous Stony Brook Algorithm Repository has a section on computational geometry which covers the basics pretty well. – MarkJ May 18 '11 at 13:18
• This would be really useful. You could restrict the algorithms to only use a limited set of spatial predicates and only work on WKT/WKB/WKR. This would keep things generic. – Matthew Snape May 18 '11 at 14:24
• @Hairy we can only moderate comments, flag posts and close or migrate questions - no special place for creating new pages. There are many stack exchange sites for code, programmers. GISse welcomes small snippets of code. If need more than a snippet of code suggest jsfiddle.net/hayley/C4rue (example) for javascript as users get live link with code and can change it interactively. – Mapperz May 18 '11 at 14:57
• It's a real shame a nice little special section couldn't be made to host a code library for folks to use - it would make this site unique and have that "something special" about it. I also think you don't have to have generic suedo code - you could break it down by company/product etc. I think generic code just wouldn't work anyway, I think you would hardly get any submissions. – Vidar Jun 23 '11 at 20:47

If you want to contribute to open source project on writing GIS API, why don't you join QGIS http://doc.qgis.org/?

As for pseudo-code, isn't this site already helpful for that kind of thing?

I agree this sort of thing would be a great thing to have. Unfortunately, it takes a lot of work and oversight my the admin/moderation team to keep everything organized and indexed/searchable which is why it probably hasn't been done yet. There are many other problems with this sort of thing which can be seen on the ESRI forums regarding their "Gallery" method which is probably one of the worst ways to do what they are trying to do. This would be very helpful to all of us developers out here though, so great idea for bringing it up.

• I am positive it would only aid people, and also enhance the site. – Hairy May 18 '11 at 12:12

Just saw that Stack Exchange has implemented a code snippets feature, whereby HTML/CSS/JavaScript code can be embedded and executed directly from the post. Not quite a code library, but interesting nonetheless.

I haven't tried this out to see if it works on GIS SE yet, but it looks promising. Edit: It is not enabled for this site, but I have asked that it be enabled.

Unfortunately, no support for other languages, like Python, although it is no surprise that supporting non-web-based languages in a web page is not an easy task.

• +1 As a Python only developer I wondered briefly about its relevance to me and came to the same conclusion. – PolyGeo Sep 18 '14 at 0:12

If you need to repeat things you have already answered (not necessary a code), you can use the answer permalink. However, I agree it would be interesting to have a code library!

• I was more looking for an area where we could just put a bit of code up, a function, or a method, or an algorithm, which would help solve problems. So many times, I have thought I have code which does X or Y, then cannot get it onto a secure site, or off a secure site, so thought an online repository of code would be a cracking idea, if collaborated on too. – Hairy May 18 '11 at 11:32

may be something like JTS (http://tsusiatsoftware.net/jts/main.html)?

I have long been a champion of code sharing, and also of keeping math specific algorithms out of a gis per se. But is there a searchable area we can have to out up a load of library functions e.g. a point in polygon search, or find perpendicular point on line, etc. I mean functions specific to GI operations. I genuinely enjoy the site, and like to encourage sharing in our communtity, but people are 'googling' for code samples first, and maybe it would be a good diea to have a repository here, that people can use?

I think it would be an excellent way to make this a more popular resource on the internet

As for generic code, I dont agree. I think we should be able to out any code up, as there aren't that many flavours and the flavours we have are easily interchangeable; migrating in between any scripting languages is fine, as it, pretty much C# <-> Java and, to be frank, it's the logic around certain issues which more interests us. I am also not talking about massive productions of code, I am talking about functions, methods which perform very specific tasks i.e. finding a perpendicular point to a line, etc. where you actually want the algorithm in code, which you can copy over and test, then use. I have a lot of code (somewhere), which I dip into every now and again to solve a problem I have seen before.

I just think it would be a great resource, and would see me, pretty much, permalinked here.

• Do you mean like the ESRI code gallery? I use it so often, I think everyone already knows about it. But perhaps not. Resources.arcgis.com – Brad Nesom May 18 '11 at 13:42
• As a point of clarification, Hairy, where you write "for generic code, I don't agree," do you mean you think code should or should not be generic? The rest of that paragraph and some of your comments elsewhere in this thread suggest you may be advocating generic code (or pseudocode), but perhaps you are really saying that language-specific code is fine because it's easily ported. This ambiguity makes it hard to understand exactly what you are proposing. – whuber Dec 15 '11 at 16:11

This is a great idea. Would be great.

Perhaps we could even use a public repository somewhere like bitbucket?