create GeoJSON for loop used? - geojson

create JSON file
Here I want to use the min and max of x and y, then I have 4 points for the polygon. But how to loop through precinct?
And I cannot get the JSON working.
z.sub <- subset(z, (z$Violation.Precinct %in% police.precincts))
z.long.min <- NULL
z.long.max <- NULL
z.lat.min <- NULL
z.lat.max <- NULL
### find coordinates for each precinct
for(i in 1:length(police.precincts)){
z.long.min[i] <- min(hulls[hulls$Violation.Precinct == police.precincts[i],]$x)
z.long.max[i] <- max(hulls[hulls$Violation.Precinct == police.precincts[i],]$x)
z.lat.min[i] <- min(hulls[hulls$Violation.Precinct == police.precincts[i],]$y)
z.lat.max[i] <- max(hulls[hulls$Violation.Precinct == police.precincts[i],]$y)
}
s <- {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[z.long.min[1], z.lat.min[1]], [z.long.min[1], z.lat.max[1]], [z.long.max[1], z.lat.min[1]], [z.long.max[1], z.lat.max[1]], [z.long.min[1], z.long.max[1]]
]
]
},
"properties": {
"precinct": 1
}
}
}

Related

Netlogo ecosystem model die command

I am trying to simulate what happens to vultures if they randomly stumble upon a carcass that has been poisoned by poachers. The poisoned carcass needs to be random. I also need to plot the deaths, so do i need to set up a dead/poisoned state in order to plot the deaths, do I need to code a to die section. Im not sure. TIA
to go
; repeat 10800 [
ask vultures
[
if state = "searching" [ search-carcasses ]
if state = "following" [follow-leaders-find-carcasses]
if state = "searching"
[ if random-float 1 < ( 1 / 360 )
[ ifelse random 2 = 0
[ rt 45 ]
[ lt 45 ] ] ]
if state != "feeding"
[ fd 0.009 ]
if state = "leader" [set time-descending time-descending + 1]
if mycarcass != 0
[ if distance mycarcass <= 0.009
[ set state "feeding"
ask mycarcass
[ set occupied? "yes" ] ] ] if state = "feeding" [
ask mycarcass
[if poisoned? = "yes"
[set state "poisoned"] ] ] if state = "poisoned" [die] ] tick ; ]

Sorting an dictionary of dictionary in swift3 based on particular key

I am receiving the following response from API, I need to sort this dictionary of dictionary based on the displayOrder key. So as per the below code you can see currently the order is [SALE, OFFER, DISCOUNT, SOLD] which I want to be in order [SALE, SOLD, OFFER, DISCOUNT].
["SALE": {
displayOrder = 1;
segId = 2;
chg = 2;
}, "OFFER": {
displayOrder = 3;
segId = 4;
chg = 2;
}, "DISCOUNT": {
displayOrder = 4;
segId = 1;
chg = 1;
}, "SOLD": {
displayOrder = 2;
segId = 2;
chg = 1;
}]
Simple sort it with sorted function like:
let dic = ["SALE": [
"displayOrder" : 1,
"segId" : 2,
"chg" : 2,
], "OFFER": [
"displayOrder" : 3,
"segId" : 4,
"chg" : 2,
], "DISCOUNT": [
"displayOrder" : 4,
"segId" : 1,
"chg" : 1,
], "SOLD": [
"displayOrder" : 2,
"segId" : 2,
"chg" : 1,
]]
dic.sorted { (lhs, rhs) -> Bool in
lhs.value["displayOrder"]! < rhs.value["displayOrder"]!
}

Is it possible to get the duration (time) video through Youtube data API v3.0

Is it possible to get the duration
(time) video through Youtube data API v3.0.
If so, how?
You will have to make a call to the Youtube Data API's Video resource after you make the search call. You can put up to 50 video id's in search, so you wont have to call it for each element.
https://developers.google.com/youtube/v3/docs/videos/list
You'll want to set part=contentDetails, because duration is there.
For example the following call:
https://www.googleapis.com/youtube/v3/videos?id=9bZkp7q19f0&part=contentDetails&key={YOUR_API_KEY}
Gives this result:
{
"kind": "youtube#videoListResponse",
"etag": "\"XlbeM5oNbUofJuiuGi6IkumnZR8/ny1S4th-ku477VARrY_U4tIqcTw\"",
"items": [
{
"id": "9bZkp7q19f0",
"kind": "youtube#video",
"etag": "\"XlbeM5oNbUofJuiuGi6IkumnZR8/HN8ILnw-DBXyCcTsc7JG0z51BGg\"",
"contentDetails": {
"duration": "PT4M13S",
"dimension": "2d",
"definition": "hd",
"caption": "false",
"licensedContent": true,
"regionRestriction": {
"blocked": [
"DE"
]
}
}
}
]
}
The time is formatted as an ISO 8601 string. PT stands for Time Duration, 4M is 4 minutes, and 13S is 13 seconds.
Please refer to this question for more details
Here is how I did it from .NET and C#.
First include the "contentDetails" part
var searchListRequest = youtubeService.Videos.List("snippet,contentDetails");
Second convert the duration to something more programmatically manageable as follows:
TimeSpan YouTubeDuration = System.Xml.XmlConvert.ToTimeSpan(searchResult.ContentDetails.Duration);
I hope this is helpful
using javascript
function converTime(d) {
//ignore the "PT" part
d = d.search(/PT/i) > -1? d.slice(2) : d;
let h, m, s;
//indexes of the letters h, m, s in the duration
let hIndex = d.search(/h/i),
mIndex = d.search(/m/i),
sIndex = d.search(/s/i);
//is h, m, s inside the duration
let dContainsH = hIndex > -1,
dContainsM = mIndex > -1,
dContainsS = sIndex > -1;
//setting h, m, s
h = dContainsH? d.slice(0, hIndex) + ":" : "";
m = dContainsM? d.slice(dContainsH ? hIndex + 1 : 0, mIndex) : dContainsH? "0" : "0";
s = dContainsS? d.slice(dContainsM ? mIndex + 1 : hIndex + 1, sIndex) : "0";
//adding 0 before m or s
s = (dContainsM || dContainsS) && s < 10? "0" + s: s;
m = (dContainsH || dContainsM) && m < 10? "0" + m + ":" : m + ":";
return d !== "0S" ? h + m + s : "LIVE"
}
console.log(converTime("PT6M7S"));

Z3 Sudoku Solver

I was on the site rise4fun a few weeks ago and they had a python code that converted a sudoku puzzle input file to z3. I checked again today and the file is gone, and was wondering if anyone had this code or could explain to me how to implement it. Thanks!!
# 9x9 matrix of integer variables
X = [ [ Int("x_%s_%s" % (i+1, j+1)) for j in range(9) ]
for i in range(9) ]
# each cell contains a value in {1, ..., 9}
cells_c = [ And(1 <= X[i][j], X[i][j] <= 9)
for i in range(9) for j in range(9) ]
# each row contains a digit at most once
rows_c = [ Distinct(X[i]) for i in range(9) ]
# each column contains a digit at most once
cols_c = [ Distinct([ X[i][j] for i in range(9) ])
for j in range(9) ]
# each 3x3 square contains a digit at most once
sq_c = [ Distinct([ X[3*i0 + i][3*j0 + j]
for i in range(3) for j in range(3) ])
for i0 in range(3) for j0 in range(3) ]
sudoku_c = cells_c + rows_c + cols_c + sq_c
# sudoku instance, we use '0' for empty cells
instance = ((5,3,0,0,7,0,0,0,0),
(6,0,0,1,9,5,0,0,0),
(0,9,8,0,0,0,0,6,0),
(8,0,0,0,6,0,0,0,3),
(4,0,0,8,0,3,0,0,1),
(7,0,0,0,2,0,0,0,6),
(0,6,0,0,0,0,2,8,0),
(0,0,0,4,1,9,0,0,5),
(0,0,0,0,8,0,0,7,9))
instance_c = [ If(instance[i][j] == 0,
True,
X[i][j] == instance[i][j])
for i in range(9) for j in range(9) ]
s = Solver()
s.add(sudoku_c + instance_c)
if s.check() == sat:
m = s.model()
r = [ [ m.evaluate(X[i][j]) for j in range(9) ]
for i in range(9) ]
print_matrix(r)
else:
print "failed to solve"

Split a map using Groovy

I want to split up a map into an array of maps. For example, if there is a map with 25 key/value pairs. I want an array of maps with no more than 10 elements in each map.
How would I do this in groovy?
I have a solution which I am not excited about, is there better groovy version:
static def splitMap(m, count){
if (!m) return
def keys = m.keySet().toList()
def result = []
def num = Math.ceil(m?.size() / count)
(1..num).each {
def min = (it - 1) * count
def max = it * count > keys.size() ? keys.size() - 1 : it * count - 1
result[it - 1] = [:]
keys[min..max].each {k ->
result[it - 1][k] = m[k]
}
}
result
}
m is the map. Count is the max number of elements within the map.
Adapting my answer to this question on partitioning a List, I came up with this method:
Map.metaClass.partition = { size ->
def rslt = delegate.inject( [ [:] ] ) { ret, elem ->
( ret.last() << elem ).size() >= size ? ret << [:] : ret
}
rslt.last() ? rslt : rslt[ 0..-2 ]
}
So if you take this map:
def origMap = [1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f']
All of the following assertions pass :-)
assert [ [1:'a'], [2:'b'], [3:'c'], [4:'d'], [5:'e'], [6:'f'] ] == origMap.partition( 1 )
assert [ [1:'a', 2:'b'], [3:'c', 4:'d'], [5:'e', 6:'f'] ] == origMap.partition( 2 )
assert [ [1:'a', 2:'b', 3:'c'], [4:'d', 5:'e', 6:'f'] ] == origMap.partition( 3 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d'], [5:'e', 6:'f'] ] == origMap.partition( 4 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d', 5:'e'], [6:'f'] ] == origMap.partition( 5 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f'] ] == origMap.partition( 6 )
Or, as a Category (to avoid having to add anything to the metaClass of Map:
class MapPartition {
static List partition( Map delegate, int size ) {
def rslt = delegate.inject( [ [:] ] ) { ret, elem ->
( ret.last() << elem ).size() >= size ? ret << [:] : ret
}
rslt.last() ? rslt : rslt[ 0..-2 ]
}
}
Then, where you need this functionality, you can simply use the Category like so:
use( MapPartition ) {
assert [ [1:'a'], [2:'b'], [3:'c'], [4:'d'], [5:'e'], [6:'f'] ] == origMap.partition( 1 )
assert [ [1:'a', 2:'b'], [3:'c', 4:'d'], [5:'e', 6:'f'] ] == origMap.partition( 2 )
assert [ [1:'a', 2:'b', 3:'c'], [4:'d', 5:'e', 6:'f'] ] == origMap.partition( 3 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d'], [5:'e', 6:'f'] ] == origMap.partition( 4 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d', 5:'e'], [6:'f'] ] == origMap.partition( 5 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f'] ] == origMap.partition( 6 )
}

Resources